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.

 

 

length of string without using strlen

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

14 comments on “Java program to toggle each character in a string”


  • Juvvala

    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());
    }
    }


  • Khushi Saxena

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    String str=””;
    for(int i=0;i<s.length();i++){
    if(Character.isUpperCase(s.charAt(i))){
    str+=Character.toLowerCase(s.charAt(i));
    }
    else{
    str+=Character.toUpperCase(s.charAt(i));
    }
    }
    System.out.print(str);
    }
    }


  • irishbellamuthan

    Identify the error(s) in the code given and rectify so that the program accepts a string as the input and prints the same.

    import java.util.Scanner;
    public class Hello {

    public static void main(String[ ] args) {
    Scanner sc = new Scanner(System.in);
    char str = sc.nextChar();
    System.out.println(“%s”,str);
    }
    }


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main (String[]args)
    {
    String str=”Gyanendra Tiwari”;

    System.out.println(find(str));
    }

    public static StringBuffer find(String str){

    StringBuffer ans=new StringBuffer();

    for(int i=0;i=’A’ && ch=’a’ && ch<='z'){
    int val=(int)str.charAt(i) – 32;
    ch=(char)val;
    }

    ans.append(ch);
    }

    return ans;
    }

    }


  • Pradipto

    class HelloWorld {
    public static void main(String[] args) {

    String str=”geek for geeks”;
    String str1[]=str.split(” “);
    String temp=””;
    for(String s:str1){

    char ch[] = s.toCharArray();

    for(int j=0;j<ch.length;j++){
    if(j==0 || j==(ch.length-1)){
    ch[j]=(char)(ch[j]+'A'-'a');
    }
    else{
    continue;
    }

    }

    temp=temp+String.valueOf(ch)+" ";

    }
    System.out.println(temp);
    }
    }


  • Pradipto

    class HelloWorld {
    public static void main(String[] args) {

    String str=”geek for geeks”;
    String str1[]=str.split(” “);
    String temp=””;
    for(String s:str1){

    char ch[] = s.toCharArray();
    //System.out.println(String.valueOf(ch));
    for(int j=0;j<ch.length;j++){
    if(j==0 || j==(ch.length-1)){
    ch[j]=(char)(ch[j]+'A'-'a');
    }
    else{
    continue;
    }

    }

    temp=temp+String.valueOf(ch)+" ";

    }
    System.out.println(temp);
    }
    }


  • Siddhesh

    import java.util.Scanner;

    public class Toggle {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter the String: “);
    String s = sc.nextLine();
    String s1 = ” “;

    for (int i = 0; i < s.length(); i++) {
    char ch = s.charAt(i);
    if (Character.isUpperCase(ch)) {
    s1 = s1 + Character.toLowerCase(ch);
    } else {
    s1 = s1 + Character.toUpperCase(ch);
    }
    }
    System.out.println("Toggle string is : " + s1);
    }


  • POOJA

    StringBuilder str1 = new StringBuilder(str);
    for (int i = 0; i = ‘a’ && ch =’A’ && ch<='Z') {
    char lws = (char) (ch + 'a' – 'A');
    str1.setCharAt(i, lws);
    }
    }


  • POOJA

    StringBuilder str1 = new StringBuilder(str);
    for (int i = 0; i = ‘a’ && ch =’A’ && ch<='Z') {
    char lws = (char) (ch + 'a' – 'A');
    str1.setCharAt(i, lws);
    }
    }
    System.out.print(str1);
    }
    }


  • Vaibhav

    package stringprogram;
    import java.util.Scanner;
    public class TogglingEachCharacter {

    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter String “);
    String Str1 = sc.next();
    String Str2=””;
    for(int i = 0;i =65 && (int)Str1.charAt(i)<=91){

    Str2=Str2+Str1.toLowerCase().charAt(i);
    }
    else {
    Str2=Str2+Str1.toUpperCase().charAt(i);
    }
    }
    System.out.println("Toggle string is "+Str2);

    }
    }


  • Vaibhav

    package stringprogram;

    import java.util.Scanner;

    public class TogglingEachCharacter {

    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter String “);
    String Str1 = sc.next();
    String Str2=””;
    for(int i = 0;i =65 && (int)Str1.charAt(i)<=91)
    Str2=Str2+Str1.toLowerCase().charAt(i);
    else
    Str2=Str2+Str1.toUpperCase().charAt(i);
    }
    System.out.println("Toggle string is "+Str2);

    }
    }