Toggle String | Competitive Programming Questions with Answers in Java

Competitive Programming Questions with Answers in Java

Toggle String

Problem

You have been given a String S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output.

Input Format

The first and only line of input contains the String S

Output Format
Print the resultant String on a single line.

Constraints
1≤|S|≤100 where S denotes the length of string S.



Sample Input
abcdE

Sample Output
ABCDe

Solution

import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {

        Scanner s = new Scanner(System.in);
        String name = s.nextLine();
        int l = name.length();
        char a[] = new char[l];
        for(int i=0; i<l; i++)
        {
            a[i] = name.charAt(i);
        
        if(Character.isLowerCase(a[i]))
        {
            a[i]=Character.toUpperCase(a[i]);
        }
        else
        {
            a[i]=Character.toLowerCase(a[i]);
        
        }
        }
        System.out.print(new String(a));
    }
}

Comments