Sat Sep 25 2021
Change Case
Java Programming1193 views
File Name: change-case.java
/* Without using toLowerCase() or toUpperCase() */
import java.io.*;
import java.util.Scanner;
class ch_case {
void upper_case(String text) {
/* Convert string to character array */
char textarr[] = text.toCharArray();
for(int i = 0; i < textarr.length; i++) {
if(textarr[i] >= 'a' && textarr[i] <= 'z')
textarr[i] = (char) (textarr[i] - 32);
}
String temp = new String(textarr);
System.out.println("In Uppercase:\n"+temp);
}
void lower_case(String text) {
/* Convert string to character array */
char textarr[] = text.toCharArray();
for(int i = 0; i < textarr.length; i++) {
if(textarr[i] >= 'A' && textarr[i] <= 'Z')
textarr[i] = (char) (textarr[i] + 32);
}
String temp = new String(textarr);
System.out.println("In Lowercase:\n"+temp);
}
}
class changecase {
public static void main(String args[ ]) {
Scanner input = new Scanner(System.in);
ch_case chc = new ch_case();
System.out.println("Enter your String:");
String text = input.nextLine();
chc.upper_case(text);
chc.lower_case(text);
}
}
/* Output */
Enter your String:
Hello World
In Uppercase:
HELLO WORLD
In Lowercase:
hello world
Author:Geekboots