将输入的单词首字母变成大写并输出。
#include <cstdio>
#include <cctype>
const int MAX_SIZE = 100 + 2;
char words[MAX_SIZE];
//#define YANGYUAN
int main()
{
#ifdef YANGYUAN
freopen("in.txt", "r", stdin);
#endif // YANGYUAN
while (fgets(words, MAX_SIZE, stdin))
{
int i = 0;
// 如果第一个字符就是字母则大写输出并将下标后移一位
if (isalpha(words[i]))
printf("%c", toupper(words[i++]));
for (; i < MAX_SIZE && words[i] != '\n' && words[i] != '\0'; ++i)
{
// 如果前一个字符是非字母而当前字符是字母说明当前字母是单词的首字母
if (0 == isalpha(words[i - 1]) && isalpha(words[i]))
printf("%c", toupper(words[i]));
else
printf("%c", words[i]);
}
printf("\n");
}
return 0;
}