icse-promo

Question

Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string.

				
					Sample INPUT: we are in cyber world
Sample OUTPUT: We Are In Cyber World
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class program6 
{
    public static void main(String[] args)
    {
        int i=0;
        char ch=' ';
        String sen="",output="";
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        sen = sc.nextLine();
        for (i = 0; i< sen.length(); i++)
        {
            ch = sen.charAt(i);
            if (i == 0 || sen.charAt(i - 1) == ' ')
            {
                output = output + Character.toUpperCase(ch);
            }
            else
            {
                output = output + ch;
            }
        }
        System.out.println("Output: " + output);
    }
}

				
			

Coding Store

Leave a Reply

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