Advertisement
BruceLe

Untitled

Jul 7th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package lab9;
  2. import java.util.Scanner;
  3. /**
  4.  *
  5.  * @author Hieu
  6.  */
  7. public class Lab9 {
  8.  
  9.     /**
  10.      * @param args the command line arguments
  11.      */
  12.     public static void main(String[] args) {
  13.         Scanner keyboard = new Scanner(System.in); 
  14.     String input;  
  15.  
  16.     System.out.print("Enter a string: ");
  17.     input = keyboard.nextLine();
  18.         System.out.print(getString(input));
  19.     }
  20.    
  21.     public static String getString(String inputString){
  22.         String result ="";
  23.         if (inputString.length()==0){
  24.             return result;
  25.         }
  26.        
  27.         char firstChar=inputString.charAt(0);
  28.         char firstCharToUpperCase=Character.toUpperCase(firstChar);
  29.         result= result+firstCharToUpperCase;
  30.        
  31.         for(int i=1; 1<inputString.length();i++){
  32.             char currentChar = inputString.charAt(i);
  33.             char previousChar = inputString.charAt(i - 1);
  34.             if (previousChar == ' ') {
  35.                char currentCharToUpperCase = Character.toUpperCase(currentChar);
  36.                result = result + currentCharToUpperCase;
  37.             }
  38.            
  39.             else {
  40.                char currentCharToLowerCase = Character.toLowerCase(currentChar);
  41.                result = result + currentCharToLowerCase;
  42.                }
  43.            
  44.         }
  45.        
  46.         return result;
  47.  
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement