Sunday, July 9, 2023

Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string.

Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string. 

Sample input:        we are in cyber world
Sample output :     We Are In Cyber World

Video Link:




Solution:
import java.util.Scanner;
public class Year2018Q1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        String str = sc.nextLine();
        str=" "+str;
        String word = "";
        for (int i = 1; i < str.length(); i++)
        {
            if (str.charAt(i - 1) == ' ')
            {
                word = word + Character.toUpperCase(str.charAt(i));
            }
            else
            {
                word = word + str.charAt(i);
            }
        }
        System.out.println(word);
    }
}

Output:

No comments:

Post a Comment

Check whether the word is Special word or Palindrome word.

Special words are those words which starts and ends with the same letter.  Examples: EXISTENCE COMIC WINDOW Palindrome words are those words...