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

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

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

by Amod Mahajan time to read: 1 min
0
%d bloggers like this: