Tuesday, 28 February 2017

Swap Case program in Java / Python


java
#############################################################
import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
       
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        char [] tempArray = new char[str.length()];
        tempArray = str.toCharArray();

        for (int i = 0; i < str.length(); i++) {
            char c = tempArray[i];
            if(Character.isUpperCase(c)){
                tempArray[i] = Character.toLowerCase(c);
            }
            else if(Character.isLowerCase(c)){
                tempArray[i] = Character.toUpperCase(c);
            }
        }
       

        System.out.println(new String(tempArray));
    }
}

#########################################################

Python( Mind the indentation)
############################################################
def swap_case(s):
    st = ''
    for i in range(len(s)):
        if s[i].isupper():
            st += s[i].lower()
        else:
            st += s[i].upper()
    return st

if __name__ == '__main__':
    s = raw_input()
    result = swap_case(s)
    print result

##############################################################

No comments:

Post a Comment