这里给出题目链接(hdu2026)
看到样例输入和输出应该就明白是怎么一回事了。
Sample Input
i like acm
i want to get an accepted
Sample Output
I Like Acm
I Want To Get An Accepted
由于存在空格,应按行读入字符数组。
先把首字母变为大写,这里使用了Character.toUpperCase()函数将字符变为大写。
随后遍历字符数组,将每个空格后的第一个字母变为大写,最后输出字符串。
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
char[] s;
while (sc.hasNext()) {
s = sc.nextLine().toCharArray();
if (s[0] != ' ')
s[0] = Character.toUpperCase(s[0]);
for (int i = 0; i < s.length; i++)
if (s[i] == ' ') {
s[i + 1] = Character.toUpperCase(s[i + 1]);
}
System.out.println(s);
}
}
}