(Coderbyte) Swap Case - Solução

Have the function SwapCase(str) take the str parameter and swap the case of each character. For example: if str is "Hello World" the output should be hELLO wORLD. Let numbers and symbols stay the way they are.

Solução:

import java.util.*;
import java.io.*;
import java.lang.*;

class Function { 
  String SwapCase(String str) {
    String resultado = "";
    for (int i = 0; i < str.length(); i++) {
      if (Character.isLetter(str.charAt(i))) {
        if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
            resultado += Character.toUpperCase(str.charAt(i));
        }
        else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
          resultado += Character.toLowerCase(str.charAt(i)); 
        }
      }
      else {
          resultado += str.charAt(i);
      }
    }
      
    return resultado;
   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.SwapCase(s.nextLine()));
  }  
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução