Java Program to replace lower-case characters with upper-case and vice-versa
Here, our task is to replace all the lower-case characters in the string to upper-case and upper-case characters to lower-case.
For this purpose, we need to traverse the string and check for each character. If the character is a lower-case character, make it upper-case by using the language-specific built-in method or add 32 to the lower-case character in C to change the ASCII value of the character.
string 1
"This Is a Comman Name"
string 2
tHIS iS A cOMMAN nAME
Class
Program
class replace_upper_lower {
public static void main(String[] args) {
//declare string
String str1="This Is a Comman Name";
for (int i=0;i //check for Lower case char if (Character.isLowerCase(str.charAt(i))) { newstr.setCharAt(i,Character.toUpperCase(str.charAt(i))); } else if(Character.isUpperCase(str.charAt(i))) { newstr.setCharAt(i,Character.toLowerCase(str.charAt(i))); } System.out.println(newstr); } }
output
tHIS iS A cOMMAN nAME
Theory
In the given program to task the given string char is convert uppercase char is lowercase and Lowercase is uppercase. it is main aim of the given program.
first of all , we can declare the any lenght of string . and one declare to StringBuffer is help to converting string are store that is orignal string is safe to after chake frist char is lowercase it is convert in Uppercase but its not store in orignal string in that resion we have require to StringBuffer. firstly we are chake each char is lowercase then the char is convert to uppercase to use toUpperCase() setCharAt() and charAt() method use in proper sequnce and is some mthod in Uppercase char. and finally print the string.
No comments:
Post a Comment