Converting first letter of each word in a sentence to upper case in Java

In this Java tutorial, we are going to convert first letter of each word to upper case in Java in a given sentence. In order to do this we have taken an input string “str” and after conversion, we stored it in another string “s” which is our output.

Header Files Used

  1. import java.io.BufferedReader:- For reading text from a string -input.stream.
  2. import java.io.Exception:- To provide exception handling code so that normal flow can be maintained.
  3. and finally, import java.io.InputStreamReader:- It reads bytes and decodes them into characters using a specified charset.

How to remove non-alphabetical characters from a string in JAVA

Guess The Number Game Using Java with Source Code



Convert first letter of each word to upper case in Java

A sentence having the first letter of each word in Capital is known as a camel case sentence. So we gonna learn how to convert a sentence to camel case sentence in Java.

Importance of using Buffer

A buffer is a place in memory to temporarily store data. Reading data from disk byte to byte is very costly means, After reading one byte we have to wait to read for another byte from disk. So to overcome this problem we use Buffer. It temporary stores data and saves time for reading data instead of reading data from disk we now read it from Buffer.

Importance of using BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.in is an Input Stream. We create an InputStreamReader which reads bytes from System.in
Then we wrap that in a Buffer Reader. So, we have a Buffer Reader that reads from an Input Stream Reader that reads from System.in

Steps we have taken to solve the Problem

  1. First, we check for the first letter of first word that whether it in lower case if it is convert it into upper case then add it to another string taken otherwise add that character as it is in another string taken.
  2. Start Reading the input string “str” from the second character because we already deal with the first character.
  3. Whenever we found an lower case alphabet and character before it is ” “(a whitespace ) which means it is the first letter of the word and convert it into upper case and after conversion we add it into another string and on the flag , otherwise if we get flag off continue reading string till its length and add it as it in another string taken.
  4. Finally, print the another string taken “s” which is now our output.

Code Snippet to check for the first character of a word in a sentence

   if(Character.isLowerCase(str.charAt(i))==true)
{
    char c=(char)(str.charAt(i-1));
       if(c==' ')
    {
         char ch= (char)(str.charAt(i)-32);
             s = s + ch;
         flag=1;
    }
}

Code to Convert the first letter of each word in a sentence to Upper case

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
public class Codespeedy
{
 public static void main(String[] args)throws IOException    
 {
     
     System.out.println("Enter the sentence");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    String s = "";
    
    int flag=0;
    
    if(Character.isLowerCase(str.charAt(0))==true)
       s=s+(char)(str.charAt(0)-32);
       
     else
         s=s+(char)(str.charAt(0));
    
    for(int i=1;i<str.length();i++)
    {
        
         if(Character.isLowerCase(str.charAt(i))==true)
        {
            char c=(char)(str.charAt(i-1));
               if(c==' ')
            {
              char ch= (char)(str.charAt(i)-32);
              s = s + ch;
               flag=1;
            }
        }
           
           if(flag==0)
        {
            s = s + str.charAt(i);
        }
          flag=0;
    }
    
    System.out.println("Output sentence after Conversion:");
    System.out.println(s);
  }
}

Output

Enter the sentence
god is one 
Output sentence after Conversion:
God Is One

So, in this code we have given input sentence “god is one” and after conversion, we got “God Is One” . First letter of word “god” i.e ‘g’ is changed to ‘G’ . Similarily , ‘i’ is changed to ‘I’ and ‘o’ is changed to ‘O’.

You may also learn,

2 responses to “Converting first letter of each word in a sentence to upper case in Java”

  1. sakshi says:

    sir , why will reading string u start with index=1??

  2. Divyesh says:

    Actually , we have deal with character at index 0 in string before for loop . And in for loop we are checking character after space which we cant do for first character of first word . and even if we start with index 0 in for loop then using (i-1) will give an error.

Leave a Reply

Your email address will not be published. Required fields are marked *