Computer Applications
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
Answer
import java.util.Scanner;
public class KboatString
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
String word = "";
for (int i = 0; i < str.length(); i++) {
if (i == 0 || str.charAt(i - 1) == ' ') {
word += Character.toUpperCase(str.charAt(i));
}
else {
word += str.charAt(i);
}
}
System.out.println(word);
}
}
Output


Related Questions
Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024Write the output of the following String methods:
String x= "Galaxy", y= "Games";
(a) System.out.println(x.charAt(0)==y.charAt(0));
(b) System.out.println(x.compareTo(y));
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)