Java Program:

- To toggle string cases

Example:

User Input: heLLo WoRLd

Output: HEllO WOrlD

import java.util.Scanner;
public class StringToggleCase{
  public static void main(String []args) {
    String s1,s2="";
    Scanner userInput = new Scanner(System.in);
    System.out.print("Enter your string : ");
    s1 = userInput.nextLine();
    for(int i=0;i<s1.length();i++) {
      if(s1.charAt(i)>='a' && s1.charAt(i)<='z')
        s2+=Character.toUpperCase(s1.charAt(i));
      else if(s1.charAt(i)>='A' && s1.charAt(i)<='Z')
        s2+=Character.toLowerCase(s1.charAt(i));
      else 
        s2+=s1.charAt(i);
    }
    System.out.println("After toggle, Your string : "+ s2);
  }
}

Output:

  Enter your string : Have good DAy
  After toggle, Your string : hAVE GOOD daY
Program
string

You should know first :

Lowercase alphabets : Starts from a (97) to z (122)

Uppercase alphabets : Starts from A (65) to z (90)


Main Logic:

We are using 2nd variable s2 to store the toggled string

for(int i=0;i<s1.length();i++) {
  if(s1.charAt(i)>='a' && s1.charAt(i)<='z') {     //Check for lowercase
    s2+=Character.toUpperCase(s1.charAt(i));  //convert it to uppercase & store it
  }
  else if(s1.charAt(i)>='A' && s1.charAt(i)<='Z') {    //Check for uppercase
    s2+=Character.toLowerCase(s1.charAt(i));     //covert it to lowercase & store it
  }
  else {
    s2+=s1.charAt(i);   //Save character other than alphabets
  }
}
//Finally, print s2 variable
Concept

Coming Soon !

Quick
Tutorial

Quote

Find a place inside where there’s joy, and the joy will burn out the pain.