Menu
Topics Index
...
`

Capitalize The First Letter Of Every Word In The Sentence


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to capitalize the first letter of every word in the sentence

Input(string) Output(string)
sun rises in the east Sun Rises In The East
hihowru? Hihowru?
He is GOING to school He Is GOING To School



class CapitalizeTheFirstLetter
{
    public static void main(String s[])
    {
        String input = "sun rises in the east";
        String output = capitalizeFirstLetters(input);
        System.out.println("Capitalized sentence is : " + output);

    }


    public static String capitalizeFirstLetters(String input)
    {
    }

}
Topic: charAt() Method In Java - Java Character Extraction

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

here we are given a sentence and we hve to capitalize frst lettr of evry word.. here we can see that frst lettr of evry chrctr cms aftr space.... as space appears , the nxt chrctr aftr it has to be convrtd to upprcase.... for this frstly we havr to cnvrt frst lettr of every strng to upper..... I.e charAt(0) has to be convrtd to uppercase..... den traverse the string wenevr spaces are found conbrt its adjcnt lettr to upprcase... ....for j=1 to input.length() den if charAt(j) =' '......  den convert charAt(j + (plus) 1) to upper case.... Input.charAt(j+1)-=32..exit loop prnt string

Posted by Asma Mujtaba Khan    2015-01-06 08:21:57


To capitalize first word of every word of a sentence we will have to use "split()" method of String class.

--String[] Split(character token):-->
Used to split the String into the array of String based on character passed as arugument to the function.

-->String op[]=input.split(" ");//this array contains words

--Then we run "FOR LOOP" till the length of array which contains words and we will have to caplitalize first letter of every String using toUpperCase() method and go on appending to the empty string.

string res="";
-->for(int i=0;i<op.length;i++)
   {
    res+=op[i].charAt(0).toUpperCase();//to capitalize first letter of each word
   }

--Then output is in variable "res".

Posted by Mânïshå Mùlchåndânï    2015-01-06 16:57:25


o capitalize first word of eve

Posted by Shubham Bansal    2015-01-06 20:05:09


here we need to make capital letter every word starting letter for these we need to split the String

*split() string by passing " " as deliminater,this method will return the array of Strings
*take an for loop to travel through the array..
now take the 1st char of every word and make it capital and append it
result=output[i].charAt(0).toUpperCase()+output[i].substring(1,output[i].length())+" ";
*trim() to remove the spaces than return

Posted by Uday Kumar    2015-01-07 16:51:24


This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Uday Kumar,  for 'Second Correct Comment'. The 'lucky liker' is Vikas Tiyagi. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-31 04:23:01


© meritcampus 2019

All Rights Reserved.

Open In App