Java provide us the inbuilt method to change the case of the string such as toLowerCase() and
toUpperCase() . But what if we need to change the case of all the characters in the string . We can achieve that target by using the simple logic .
Before we go ahead we need to know about the UNICODE . A unicode is the method of representing characters as an integer . Unlike ascii which is a 7 bit representation , unicode is the 16 bit representation of each character .
For lowercase letters that is for a,b,c,d.....x,y,z
the unicode values lies in the range of 97,98,99,.......121,122
For uppercase letters that is A,B,C,D ...... X,Y,Z
the unicode values lies in the range of 65,66,67......89,90
Logic is that we check the unicode of the character , if the unicode
lies between 97 to 122 then subtract 32 from that so that it will automatically be converted from lowercase to uppercase unicode integer representation of character while if the unicode lies between 65 to 90 then add
32 from that number so that it will automatically be converted from uppercase to lowercase unicode integer representation of character .
Demo :
Please find the code below :
toUpperCase() . But what if we need to change the case of all the characters in the string . We can achieve that target by using the simple logic .
Before we go ahead we need to know about the UNICODE . A unicode is the method of representing characters as an integer . Unlike ascii which is a 7 bit representation , unicode is the 16 bit representation of each character .
For lowercase letters that is for a,b,c,d.....x,y,z
the unicode values lies in the range of 97,98,99,.......121,122
For uppercase letters that is A,B,C,D ...... X,Y,Z
the unicode values lies in the range of 65,66,67......89,90
Logic is that we check the unicode of the character , if the unicode
lies between 97 to 122 then subtract 32 from that so that it will automatically be converted from lowercase to uppercase unicode integer representation of character while if the unicode lies between 65 to 90 then add
32 from that number so that it will automatically be converted from uppercase to lowercase unicode integer representation of character .
Demo :
Please find the code below :
public class ChangeCase { static int i; static void changecase(String s) { for(i=0;i<s.length();i++) { int ch=s.charAt(i); if(ch>64&&ch<91) { ch=ch+32; System.out.print( (char) ch); } else if(ch>96&&ch<123) { ch=ch-32; System.out.print( (char) ch); } if(ch==32) System.out.print(" "); } } public static void main (String args[]) { System.out.println("Original String is : "); System.out.println("Alive is awesome "); ChangeCase.changecase("Alive is awesome "); } }