3 Sentence Capitalizer Write a method that accepts a String

3. Sentence Capitalizer

Write a method that accepts a String object as an argument and returns a copy of the string

with the first character of each sentence capitalized. For instance, if the argument is “hello.

my name is Joe. what is your name?” the method should return the string “Hello. My name

is Joe. What is your name?” Demonstrate the method in a program that asks the user to input a string and then passes it to the method. The modified string should be displayed on

the screen.

Solution

FirstLetterCapitalTest.java

import java.util.Scanner;


public class FirstLetterCapitalTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter a string: \");
       String s = scan.nextLine();
       String result = makeCapital(s);
       System.out.println(result);
   }
  
   public static String makeCapital(String s){
       String str =\"\";
       boolean found = false;
       for(int i=0; i<s.length(); i++){
           char ch = s.charAt(i);
           if(i == 0 || (found && ch != \' \')){
               str = str + String.valueOf(ch).toUpperCase();
               found = false;
           }
           else{
               if(ch == \'.\'){
                   found = true;
               }
               str = str + ch;  
              
           }
       }
       return str;
   }

}

Output:

Enter a string:
hello. my name is Joe. what is your name?
Hello. My name is Joe. What is your name?

3. Sentence Capitalizer Write a method that accepts a String object as an argument and returns a copy of the string with the first character of each sentence ca

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site