Skip to content

Commit b9458a1

Browse files
Create 35.2 Capitalize Words.java
1 parent 963c71d commit b9458a1

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

35.2 Capitalize Words.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
In an activity conducted in a kids school, the students are told to carefully read the sentence written on the white board and convert each word's first alphabet to capital. Note that the first letter of the last word should be small.
3+
4+
Input Format
5+
6+
Input takes a String containing words separated with spaces.
7+
8+
Constraints
9+
10+
String with no " " delimiter should display "ERROR" message.
11+
12+
Output Format
13+
14+
The string in the output should have first word capitalized for each word, except the last word.
15+
16+
Sample Input 0
17+
18+
Sia sings a song
19+
Sample Output 0
20+
21+
Sia Sings A song
22+
*/
23+
import java.io.*;
24+
import java.util.*;
25+
26+
public class Solution {
27+
28+
public static void main(String[] args) {
29+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
30+
Scanner sc = new Scanner(System.in);
31+
String st;
32+
try
33+
{
34+
st = sc.nextLine();
35+
String []s = st.split(" ");
36+
if(s.length==1)
37+
{
38+
System.out.print("ERROR");
39+
40+
}
41+
else
42+
{for(int i=0;i<s.length-1;i++)
43+
{
44+
String f = s[i].substring(0,1);
45+
f = f.toUpperCase();
46+
s[i] = s[i].substring(1);
47+
s[i] = f+s[i];
48+
}
49+
String l = s[s.length-1].substring(0,1);
50+
l = l.toLowerCase();
51+
s[s.length-1] = s[s.length-1].substring(1);
52+
s[s.length-1] = l+s[s.length-1];
53+
for(int i=0;i<s.length;i++)
54+
System.out.print(s[i]+" ");}
55+
}
56+
catch(Exception e)
57+
{
58+
System.out.print("ERROR");
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)