Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. The program should let the user enter a string and then pass it to the function, printing out the modified string.

Respuesta :

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 System.out.println("Enter String");

 Scanner sc1=new Scanner(System.in);

 String st1=sc1.nextLine();

 convertfirstchartoupper(st1);

 //System.out.println(st1);

 

}

private static void convertfirstchartoupper(String st1){

     

      char[] arr = st1.toCharArray();

      int k =0;

      arr[0]=Character.toUpperCase(arr[0]);

      while(k<arr.length)

      {

         

         if(Character.isWhitespace(arr[k]))

         {

             

             arr[k+1]=Character.toUpperCase(arr[k+1]);

             

         }

         

         

        k++;

      }

      k=0;

       while(k<arr.length)

      {

         

        System.out.print(arr[k]);

         

        k++;

      }

  }

}

Explanation:

In the above java program, we change a string into an array of characters and then check for whitespace. the letter next to while space is the first letter of the new word, and hence we capitalize that letter. And this is what is required. However, we can split the sentence into a string of words as well, and then capitalize the first letter of each word.

ACCESS MORE