Make Selenium Easy

Frequently Asked Java Program 36 : Java Program to Invert Case of Each Char in a String

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to invert the case of each character of string.

Example:-

Input: – mAkE SeleNIUM eaSY

Output:-MaKe sELEnium EAsy

Let’s start with the logic:-

  1. Iterate entire string char by char.
  2. Check if current char is in uppercase or Lowercase. We can use isUpperCase(Character char) and isLowerCase(Character char) methods of Character class. These methods return boolean value. We can also use ASCII concept as well. In this post, we will use methods of Character class.
  3. Once we find the case type of current character, we can use toUpperCase(Character c) and toLowerCase(Character c) methods of Character class to invert the case and replace the content of char array at given index.
  4. To convert a char array to string again, we can use overloaded constructor of String class which takes char[] as argument.

Java Program:-

Output:-

You can run above program for multiple inputs and if it fails for any condition, let me know.

#HappyCoding

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.