top of page

Java Program to Toggle the String

In this blog, we will learn how to toggle the character of given String. That means if a character is in lowercase then it will be converted(toggled) to uppercase and vice versa.


Implementation using Java:

public class ToggleString {
	public static String toggle(String str)
	{
		String words[]= str.split("\\s");
		String toggle=" ";
		for(String w: words)
		{
			String first=w.substring(0,1);
			String second=w.substring(1);
			    	 toggle+=first.toLowerCase()+second.toUpperCase();
		}
		return toggle.trim();
	}
        public static void main(String[] args) {
		System.out.println(toggle("This Is Programmers Door"));
       }
}
Input: This Is Programmers Door
Output: tHIS iS pROGRAMMERS dOOr
 

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor


Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

28 views0 comments

Recent Posts

See All
bottom of page