Java Program - convert upper to lower and lower to upper for given string
Question:
Write a Java program to convert upper to lower and lower to upper character for given String.
Input: "SeleniumTesting"
Output: "sELENIUMtESTING"
Steps:
Step 1: Initialize the input string in main method;
Step 2: Create charConversion() method;
Step 3: Initialize the StringBuffer object;
Step 4: Iterate the for loop to check whether the given character is lower or upper using isLowerCase() and isUpperCase();
Step 5: Convert the character and add it into Stringbuffer using setCharAt() method;
Step 6: Finally print the output
public class lowerAndUpperConversion {
public static void main(String[] args) {
String name ="SeleniumTesting";
charConversion(name);
}
public static void charConversion(String name){
StringBuffer strBuff=new StringBuffer(name);
//Logic too convert the lower to upper and upper to lower
for(int i=0;i<name.length();i++){
if(Character.isLowerCase(name.charAt(i))){
strBuff.setCharAt(i,Character.toUpperCase(name.charAt(i)));
}else if(Character.isUpperCase(name.charAt(i))){
strBuff.setCharAt(i,Character.toLowerCase(name.charAt(i)));
}
}
System.out.println(strBuff);
}
}
Output:
sELENIUMtESTING
Comments
Post a Comment