Write a program that accepts as input a sentence in which al

Write a program that accepts as input a sentence in which all of the words are run together, but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string \"StopAndSmellTheRoses.\" would be converted to \"Stop and smell the roses.\"

Solution

WordSeparator.java

import java.util.Scanner;


public class WordSeparator {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the sentence: \");
       String s = scan.next();
       String resultStr = \"\";
       for(int i=0; i<s.length(); i++){
           char ch = s.charAt(i);
           if(i == 0){
               resultStr = resultStr + String.valueOf(ch).toUpperCase();
           }
           else{
               if(Character.isUpperCase(ch)){
                   resultStr = resultStr + \" \";
               }
               resultStr = resultStr + String.valueOf(ch).toLowerCase();
           }
       }
       System.out.println(resultStr);
   }

}

Output:

Enter the sentence: StopAndSmellTheRoses.
Stop and smell the roses.

 Write a program that accepts as input a sentence in which all of the words are run together, but the first character of each word is uppercase. Convert the sen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site