Here, we are supposed to create a source code that asks for String input from the user and displays the string by changing the String’s Case to both UpperCase and LowerCase.

The basic idea is to change the input string’s case using ASCII Values.

The ASCII Values are:
A-Z = 65 to 90
a-z = 97 to 122
0-9 = 48 to 57

Algorithm:
1. Enter and Store String Input.
2. Using a Loop-construct, scan each character of the string and check.
3. If said character is in Lower case, i.e. ASCII Values are within 97 and 122, then subtract 32 from their ASCII Values and typecast it back from Integer to Character.
Otherwise, If said character is in Upper case, i.e. ASCII Values are within 65 and 90, then add 32 from their ASCII Values and typecast it back from Integer to Character.
4. Keep Accumulating these characters in another string that will display the final results: one for Upper Case and one from Lower Case.
5. Display the Results Accordingly.

Note: Suppose char x = ‘T’ and char s = ‘t’.
int n1=x; this will store the ASCII value of ‘T’ in n1.
Also, the difference between ASCII values of any Alphabet in both the case will be of 32. E.g. ASCII of ‘A’: 65 and ‘a’: 97, the difference has to be 32 in every case.

Source Code:

/* Program to enter a string and display in both UpperCase and LowerCase using ASCII Values.*/

import java.util.*;
class ChangeCase
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter String: ");
String s=inp.nextLine();
int i,n;
char c;
String z="";

for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=97 && c<=122)    //If ASCII values represent LowerCase, changing to Upper Case
{
n=c-32;
c=(char)n;
}
z=z+c;
}
System.out.println("\nUpper Case: "+z);
z="";

for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=65 && c<=90)    //If ASCII values represent UpperCase, changing to Lower Case
{
n=c+32;
c=(char)n;
}
z=z+c;
}
System.out.println("\nLower Case: "+z);
z="";

}
}

Output:

Enter String: World Wide Web

Upper Case: WORLD WIDE WEB

Lower Case: world wide web


Enter String: Great Wall of China

Upper Case: GREAT WALL OF CHINA

Lower Case: great wall of china

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019