Home > Blockchain >  Convert characters of a string to opposite case
Convert characters of a string to opposite case

Time:10-29

//HERE WHY IS IT CONVERTING TO LOWER CASE BUT NOT TO UPPER CASE

public class LowerUpperCase {
    
    public static void main(String[] args) {
        String s="lowerUppercase";
       
        
        
        for (int i = 0; i < s.length()-1; i  ) {

            if(s.charAt(i) >='a' && s.charAt(i)<='z'){  //OR IF DONE THIS NOT WORKING
```
   if((s.charAt(i) >='a' && s.charAt(i)<='z' ) || (s.charAt(i) >='A' && s.charAt(i)<='Z')){
```

               
                 s=s.toLowerCase();//here should i go for 

            }
            else if(s.charAt(i) >='A' && s.charAt(i)<='Z'){
               
                s=s.toUpperCase();

           }
           
            
        }
        System.out.println(s);
    }
}

//I ALWAYS GET OUTPUT LIKE loweruppercase or LOWERUPPERCASE

INPUT:LowerUpperCase
OUTPUT:lOWERuPPERcASE  expected

HERE WHY IS IT CONVERTING TO LOWER CASE BUT NOT TO UPPER CASE 

I ALWAYS GET OUTPUT LIKE loweruppercase or LOWERUPPERCASE

CodePudding user response:

Your code seems on the righ track, but you need to toggle between both lower and upper case. Try this version:

String s = "lowerUppercase";
StringBuilder output = new StringBuilder();

for (int i=0; i < s.length(); i  ) {
    if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
        output.append(Character.toUpperCase(s.charAt(i)));
    }
    else {
        output.append(Character.toLowerCase(s.charAt(i)));
    }
}

System.out.println(output.toString());
// LOWERuPPERCASE

Note that the above assumes that you would only have lower and upper case English alphabet letters in the input string. If not, then the answer might have to change.

CodePudding user response:

You now know why. Here is yet another way you could do it:

String s = "Lower to UpperCase";

StringBuilder sb = new StringBuilder("");
for (int i = 0; i < s.length(); i  ) {
    char c = s.charAt(i);
    if (Character.isAlphabetic(c)) { 
        if (Character.isLowerCase(c)) {
             sb.append(Character.toUpperCase(c));
        }
        else {
            sb.append(Character.toLowerCase(c));
        }
    }
    else {
        sb.append(c);
    }
}
s = sb.toString();
System.out.println(s);

This will output to the Console Window:

lOWER TO uPPERcASE

CodePudding user response:

Because you are calling the method on the entire string while you only wanna switch the case of the char at position i. Your code example sets the entire string to lowercase if the last letter is lowercase and to Uppercase in the other case.

Also String in Java is an immutable object, meaning that you cannot change its value once it's created, so you need a stringBuilder.

  public static void main(String[] args) {
        String s = "lowerUppercase";
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < s.length() - 1; i  ) {
            if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
                sb.append(Character.toUpperCase(s.charAt(i)));
            } else {
                sb.append(Character.toLowerCase(s.charAt(i)));
            }
        }
        System.out.println(sb);
    }

If you'd like to play with functional programming here's the functional version (It's not practical or anything, purely educational) :

    public static void main(String[] args) {
        String s = "lowerUppercase";

        StringBuilder out = s.chars()
                .map(i -> {
                    if (Character.isLowerCase(i)) {
                        return Character.toUpperCase(i);
                    } else if (Character.isUpperCase(i)) {
                        return Character.toLowerCase(i);
                    }
                    return i;
                }).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append);

        System.out.println(out);
    }
  • Related