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