Question:
Write a program to convert the first character of each word in a sentence to uppercase.
If the first character of each word in the given sentence is already in upper case, then print “First character of each word is already in uppercase”.
Sample Input 1:
Enter the String:
Work hard to get what you like
Sample Output 1:
Work Hard To Get What You Like
Sample Input 2:
Enter the String:
Work Hard To Get What You Like
Sample Output 2:
First character of each word is already in uppercase
Code:
InitCap.java
import java.util.*; public class InitCap { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the String:"); char[] str=sc.nextLine().toCharArray(); int upps=0,word=0; int len=str.length; for(int i=0;i<len;i++) { if(i==0 || Character.isWhitespace(str[i-1])) {word++; if(Character.isLowerCase(str[i])) { str[i]=Character.toUpperCase(str[i]); } else { upps++; } } } if(word==upps) { System.out.println("First character of each word is already in uppercase"); } else { for(char i : str) { System.out.print(i); } } } }
Recommended:
- Array square
- Generate number using odd digits
- Alternate Number Difference
- Next Greatest number
- Mark Comparison
- Print the characters in descending order
- Vowels in a fishBowl
- Least offer
- Ascending and descending order
- Mail Domain
- Count repeating words
- Sentence – Convert to upper and lower
- Count consecutive repeating characters
- Zig zag Array
- Pass and Fail Count
- Search a Course
- Average and Grade Calculation
- String – Find and replace the character (first occurrence)
- Sort the first and second half of an array
- Retail Shop
- Palindrome
- Numerology
- InitCap
- Array Compatiblilty
- Sum of the maximum and the minimum element
- String Concatenation
- Find Average Age
- Login