Toggle String : HackerEarth Problem Solution

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.

Sample Input
abcdE

Sample Output
ABCDe

C Implementation:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *s;
    s=(char*)malloc(sizeof(char)*100);
    scanf("%s",s);
    while(*s)
    {
     if(*s>='a' && *s<='z')
      printf("%c",((*s)-32));
     else
      printf("%c",((*s)+32));
     s++;
    }
    return 0;
}

C++ Implementation:

#include <iostream>
using namespace std;

int main()
{
    string s;
    cin>>s;
    for(int i=0;s[i];i++)
     if(s[i]>='a' && s[i]<='z')
      printf("%c",(~(1<<5))&s[i]);
     else
      printf("%c",((1<<5)|s[i]));
    return 0;
}

Java Implementation:

import java.io.*;
import java.lang.*;
import java.util.*;

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

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        for(int i=0;i<line.length();i++)
        {
         char ch = line.charAt(i);
         if(ch>='a' && ch<='z')
          System.out.print((char)(ch-32));
         else
          System.out.print((char)(ch+32));
        }
    }
}

Python Implementation:

import sys
s = raw_input()
for i in s:
 if i>='a' and i<='z':
  sys.stdout.write(chr(ord(i)-32))
 else:
  sys.stdout.write(chr(ord(i)+32))