Java program to toggle each character in a string
Toggle each character in a String
In this java program we’re going to make a program to toggle each character in a String , First we will take String input from user .
If the letter is in uppercase we will convert it to lowercase and if it is in uppercase we will convert it to lowercase.

Algorithm
- Take String input from user and store in the variable “s” (in this case)
- Take another String variable s1 (in this case)
- Take a for loop i start from i=0 to i<s.length
- Take if part get one by one character from String using charAt() method and and check whether is it upper case using isUpperCase() and change upper case character to lower case and store it in s
- In else part change the variable from lower case to upper case and store it in s variable
Code in Java
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { String s = "PrePInsTa"; String s1 = ""; for (int i = 0; i < s.length(); i++) { if(Character.isUpperCase(s.charAt(i))) s1=s1+Character.toLowerCase(s.charAt(i)); else s1=s1+Character.toUpperCase(s.charAt(i)); } System.out.println("Toggle String is : "+s1); } }
Output
Enter a String : PrePInsTa Toggle String is : pREpiNStA
Method 2
The Logic is same here we are using the ASCII value
- Initialize the variables.
- Initiate a for loop.
- Toggle each character.
- Terminate the loop.
- Print toggled string.
Run
public class Main { public static void main(String[] args) { String str1 = "PrepInsta"; char str[] = str1.toCharArray(); for (int i = 0; i < str1.length(); i++) { if (str[i] >= 'A' && str[i] <= 'Z') str[i] = (char)(str[i] + 'a' - 'A'); else if (str[i] >= 'a' && str[i] <= 'z') str[i]= (char)(str[i] + 'A' - 'a'); } System.out.println("Toggled string: "); for (int i = 0; i < str1.length(); i++) { System.out.print(str[i]); } } }
Toggled string: pREPiNSTA
Method 3
This program is the same as above, but this time we are using the While Loop.
Run
public class Main { public static void main(String[] args) { String str1 = "PrepInsta"; char str[] = str1.toCharArray(); int i = 0; while (i < str1.length()) { if (str[i] >= 'a' && str[i] <= 'z') { str[i] = (char)(str[i] + 'A' - 'a'); } else if (str[i] >= 'A' && str[i] <= 'Z') { str[i] = (char)(str[i] + 'a' - 'A'); } i++; } System.out.println("Toggled string: "); for (i = 0; i < str1.length(); i++) { System.out.print(str[i]); } } }
Output
Toggled string: pREPiNSTA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
public class Touglestring {
public static void main(String[] args) {
String str=”SanJIt kuMAR”;
System.out.println(str);
StringBuilder sb = new StringBuilder(str);
for(int i=0;i= 97) {
flag = false;
}
if (flag == true) {
asci += 32;
char ch1 = (char) asci;
str = str.substring(0, i) + ch1 + str.substring(i + 1);
}else {
asci -= 32;
char ch1=(char)asci;
str=str.substring(0,i)+ch1+str.substring(i+1);
}
}
System.out.println(str);
}
}
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String str=sc.next();
String s1=””;
for(int i=0;i<str.length();i++){
if(Character.isUpperCase(str.charAt(i))){
s1=s1+(char)(str.charAt(i)+32);
}
else{
s1=s1+(char)(str.charAt(i)-32);
}
}
System.out.println(s1);
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println(“enter the char”);
Scanner sc = new Scanner(System.in);
String st ;
st = sc.nextLine();
for(int i=0;i=97 && st.charAt(i)=65 && st.charAt(i)<=90){
char c = (char) (currentChar + 32);
System.out.print(c);
}
}
String euu="shivakesava";
System.out.println(euu.toUpperCase());
}
}
Hey Juvvala! Kindly join our discord server for all the technical doubts: Discord