将输入的单词首字母变成大写并输出。
#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;
}

本文介绍了一个简单的C++程序,该程序能够读取一行文本,并将每个单词的首字母转换为大写形式输出。通过使用标准C++库函数如isalpha()和toupper()来实现这一功能。

433

被折叠的 条评论
为什么被折叠?



