String - Inverse Case
The concept of doing the Inverse Case on a given string is simply to determine if a found letter is in what case and converting it to its inverse case, leaving the none Alpha characters alone.
In this program, each letter is converted to its ASCII value, determine where it is located, if capital, add 32, if not, subtract 32. It can be considered capital if it is within 65 to 90, since the ascii values of capital letters are within this range. For small letters, 97 to 122 will be the range. So basically, the range between a letter and its opposite case is 32.
Here is the code.
import java.io.*;
public class InverseCase
{
public static void main(String args[]) throws IOException
{
BufferedReader k=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string : ");
String str=k.readLine();
System.out.print("\nResult : ");
for(int i=0; i
{
int c=str.charAt(i);
if(c>=65 && c<=90)
c+=32;
else if(c>=97 && c<=122)
c-=32;
System.out.print((char)c);
}
System.out.println("");
}
}
0 comments:
Post a Comment