-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapitalizeWord.java
More file actions
36 lines (28 loc) · 1.17 KB
/
CapitalizeWord.java
File metadata and controls
36 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*Question 4: Capitalize Words
* Write a program that accepts a string as input , capitalizes the first letter of each word in the string, and then return the result string.
*/
import java.util.Scanner;
public class CapitalizeWord {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string : ");
String sent = input.nextLine();
input.close();
// calls the method and prints the result
String result = capitalizeFirstLetter(sent);
System.out.println("Capitalized String :\n " + result);
}
// Method to capitalize the first letter of each word in the string.
public static String capitalizeFirstLetter(String sent) {
// split the string into words
String[] words = sent.split(" ");
// capitalize the first letter of each word
String result = "";
for (String word : words) {
String firstLetter = word.substring(0, 1).toUpperCase();
String remainingLetters = word.substring(1).toLowerCase();
result += firstLetter + remainingLetters + " ";
}
return result.trim();
}
}