• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
December 06, 2022 |5.6K Views

Java program to convert First Character Uppercase in a Sentence

  Share  1 Like
Description
Discussion

In this video, we will write a Java program to convert the first character to uppercase in a sentence.

Examples:

Input:  gEEKs
Output: Geeks

Approach to Convert the first character to uppercase in a sentence

  1. Using character conversion
  2. Using Java Inbuilt methods
     

Method 1: Using character conversion

Step 1: Create a char array of a given String.
Step 2: If the first character of a word is found in uppercase also print.
else If it's in lower- case then convert it into Upper- case using ch( i) = ( char)( ch( i)-' a'' A')
Step 3: Apart from the first character if anyone is in Uppercase also convert it into LowerCase.
Step 4: Print string.

Method 2:  Java Inbuilt methods

To capitalize each word, call the toLowerCase() system to put the string in lowercase format. Iterate the string, if any space is found in the previous iteration and the current element isn't space also call the toUpperCase() method to put the first letter of the word in uppercase format and add the string in the buffer.