Write a Java Program to Toggle All Character cases in a String with an example. In this Java toggle Uppercase to Lowercase & Lowercase to Upper example, we used for loop to iterate StrToToggle. Inside the loop, we used the Else If statement.
Within the first if condition, we used isUpperCase ( (Character.isUpperCase (StrToToggle.charAt(i))) to check the character at the index position is uppercase. If True, we used the toLowerCase ( Character.toLowerCase( StrToToggle.charAt(i) ) to convert it to a lowercase character. Next, we used isLowerCase() ( Character.isLowerCase( StrToToggle.charAt(i))) to check the character is lowercase. If True, we used the toUpperCase() ( Character.toUpperCase ( StrToToggle.charAt(i)) ) to convert it to the uppercase character.
import java.util.Scanner; public class ToggleCharsinStr1 { private static Scanner sc; public static void main(String[] args) { String StrToToggle, tCaseStr = ""; sc= new Scanner(System.in); System.out.print("\nEnter Any String to Toggle Case = "); StrToToggle = sc.nextLine(); for(int i = 0; i < StrToToggle.length(); i++) { if(Character.isUpperCase(StrToToggle.charAt(i))) { tCaseStr += Character.toLowerCase(StrToToggle.charAt(i)); } else if(Character.isLowerCase(StrToToggle.charAt(i))) { tCaseStr += Character.toUpperCase(StrToToggle.charAt(i)); } else { tCaseStr += StrToToggle.charAt(i); } } System.out.println("\nThe Final String after Toggling all Characters = " + tCaseStr); } }
In this Java toggle character case of a string example, we first converted the StrToToggle string to the CharArrToToggle character array using the toCharArray().
Within the loop, we check whether the character at index position CharArrToToggle[i] is greater than or equal to a and less than or equal o z. If it is true, we are subtracting 32 to the current ASCII value. Next, we check the character is greater than or equal to A and less than or equal o Z. If it is True, we are adding 32 to the current ASCII value. For example, A = 65, after adding 32, it becomes 97, and the ASCII value of a = 97.
import java.util.Scanner; public class ToggleCharsinStr2 { private static Scanner sc; public static void main(String[] args) { String StrToToggle; sc= new Scanner(System.in); System.out.print("\nEnter Any String to Toggle Case = "); StrToToggle = sc.nextLine(); char[] CharArrToToggle = StrToToggle.toCharArray(); for(int i = 0; i < CharArrToToggle.length; i++) { if(CharArrToToggle[i] >= 'a' && CharArrToToggle[i] <= 'z') { CharArrToToggle[i] = (char) ((int)CharArrToToggle[i] - 32);; } else if(CharArrToToggle[i] >= 'A' && CharArrToToggle[i] <= 'Z') { CharArrToToggle[i] = (char) ((int)CharArrToToggle[i] + 32);; } } System.out.println("\nThe Final String after Toggling all Characters = "); System.out.print(CharArrToToggle); } }
Toggle string character cases output
Enter Any String to Toggle Case = JavaProgrAMS
The Final String after Toggling all Characters =
jAVApROGRams
Java Program to Toggle All Characters in a String Example 3
Here, we declared an empty character and assigned the case converted character to ch. Then, we set each character to tCaseStr.
import java.util.Scanner; public class ToggleCharsinStr3 { private static Scanner sc; public static void main(String[] args) { String StrToToggle, tCaseStr = ""; sc= new Scanner(System.in); System.out.print("\nEnter Any String to Toggle Case = "); StrToToggle = sc.nextLine(); char ch = '\0'; for(int i = 0; i < StrToToggle.length(); i++) { if(StrToToggle.charAt(i) >= 'a' && StrToToggle.charAt(i) <= 'z') { ch = (char) (StrToToggle.charAt(i) - 32); } else if(StrToToggle.charAt(i) >= 'A' && StrToToggle.charAt(i) <= 'Z') { ch = (char) (StrToToggle.charAt(i) + 32); } else { ch = StrToToggle.charAt(i); } tCaseStr += ch; } System.out.println("\nThe Final String after Toggling all Characters = " + tCaseStr); } }
Toggle string characters cases output
Enter Any String to Toggle Case = TutoRIAl GateWAy
The Final String after Toggling all Characters =
tUTOriaL gATEwaY
In this code, to toggle all character cases in a String, we are comparing the ASCII values instead of comparing characters.
import java.util.Scanner; public class ToggleCharsinStr4 { private static Scanner sc; public static void main(String[] args) { String StrToToggle, tCaseStr = ""; sc= new Scanner(System.in); System.out.print("\nEnter Any String = "); StrToToggle = sc.nextLine(); char ch = '\0'; for(int i = 0; i < StrToToggle.length(); i++) { if(StrToToggle.charAt(i) >= 65 && StrToToggle.charAt(i) <= 90) { ch = (char) (StrToToggle.charAt(i) + 32); } else if(StrToToggle.charAt(i) >= 97 && StrToToggle.charAt(i) <= 122) { ch = (char) (StrToToggle.charAt(i) - 32); } else { ch = StrToToggle.charAt(i); } tCaseStr += ch; } System.out.println("\nThe Final String after Toggling all Characters = " + tCaseStr); } }
Enter Any String = HappY coDINg
The Final String after Toggling all Characters = hAPPy COdinG
This Java program for toggle string character cases, we converted the string CharArrToToggle. And the remaining is the same as the above.
import java.util.Scanner; public class ToggleCharsinStr5 { private static Scanner sc; public static void main(String[] args) { String StrToToggle; sc= new Scanner(System.in); System.out.print("\nEnter Any String = "); StrToToggle = sc.nextLine(); char[] CharArrToToggle = StrToToggle.toCharArray(); for(int i = 0; i < CharArrToToggle.length; i++) { if(CharArrToToggle[i] >= 65 && CharArrToToggle[i] <= 90) { CharArrToToggle[i] = (char) ((int)CharArrToToggle[i] + 32);; } else if(CharArrToToggle[i] >= 97 && CharArrToToggle[i] <= 122) { CharArrToToggle[i] = (char) ((int)CharArrToToggle[i] - 32);; } } System.out.println("\nThe Final String after Toggling all Characters = "); System.out.print(CharArrToToggle); } }
Toggle characters output
Enter Any String = TogglE CharaCTERs
The Final String after Toggling all Characters =
tOGGLe cHARActerS
It is another Java example to toggle string character cases.
import java.util.Scanner; public class ToggleCharsinStr6 { private static Scanner sc; public static void main(String[] args) { String StrToTog; sc= new Scanner(System.in); System.out.print("\nEnter Any Text = "); StrToTog = sc.nextLine(); char[] CharArrToTog = StrToTog.toCharArray(); for(int i = 0; i < CharArrToTog.length; i++) { if(CharArrToTog[i] >= 'a' && CharArrToTog[i] <= 'z') { CharArrToTog[i] = (char) ((int)CharArrToTog[i] + 'A' - 'a');; } else if(CharArrToTog[i] >= 'A' && CharArrToTog[i] <= 'Z') { CharArrToTog[i] = (char) ((int)CharArrToTog[i] + 'a' - 'A');; } } System.out.println("\nThe Final String after Toggling all Characters = " ); System.out.print(CharArrToTog); } }
Enter Any Text = Hello224 Java PROGRammErs!
The Final String after Toggling all Characters =
hELLO224 jAVA progrAMMeRS!