Given a string, the task is to capitalize the first character of each word in the string. A word is defined as a sequence of non-space characters. The function should return the resulting string with each word’s first character capitalized.
A simple approach involves splitting the text by spaces to get an array of words. We then capitalize the first character of each word and append it, followed by a space, to a StringBuilder. Lastly, we convert the StringBuilder to a string and return it.
// Java program to capitalize first character of each word in a StringclassSolution {
public String capitalize(String str) {
String[] words = str.split("\\s");
StringBuilder sb =new StringBuilder();
for (String word: words) {
if (!word.isEmpty()) {
sb.append(Character.toUpperCase(word.charAt(0)));
sb.append(word.substring(1));
}
sb.append(" ");
}
// trim() to remove extra space in the end before returningreturn sb.toString().trim();
}
publicstaticvoidmain(String[] args)
{
String sentence ="k5kc is awesome!";
String str = capitalize(sentence);
System.out.println(str);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
// Method to capitalize first character of each word in the given textpublic String capitalizeFirstChar(String sentence) {
return Pattern.compile("\\s")
.splitAsStream(sentence)
.map(this::capitalize)
.collect(Collectors.joining(" "));
}
// Helper method to capitalize the first character of a wordpublic String capitalize(String s) {
if (s.equals(""))
return s;
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
// Method to capitalize first character of each word in the given textpublic String capitalizeFirstChar(String sentence) {
return Stream.of(sentence.split("\\s"))
.map(this::capitalize)
.collect(Collectors.joining(" "));
}
// Helper method to capitalize the first character of a wordpublic String capitalize(String s) {
if (s.equals(""))
return s;
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
classSolution {
// Method to capitalize first character of each word in the given textpublic String capitalizeFirstChar(String sentence) {
return Joiner.on(' ')
.join(Iterables.transform(Splitter.on(' ').split(sentence),
this::capitalize));
}
// Helper method to capitalize the first character of a wordpublic String capitalize(String s) {
if (s.equals(""))
return s;
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
1
2
3
4
5
classSolution:
defcapitalizeFirstChar(self, text: str) -> str:
words = text.split()
capitalized_words = [word.capitalize() for word in words]
return' '.join(capitalized_words)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
classSolution {
// Method to capitalize the first character of each word in the given textpublic String capitalizeFirstChar(String str) {
StringBuffer sb =new StringBuffer();
Matcher matcher = Pattern.compile("\\b(\\w)").matcher(str);
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
}
1
2
3
4
5
6
import re
classSolution:
# Method to capitalize the first character of each word in the given textdefcapitalizeFirstChar(self, text: str) -> str:
return re.sub(r'\b\w', lambda x: x.group().upper(), text)