您的位置:首页 > 其它

HDOJ 2026 首字母变大写

2017-12-20 20:27 239 查看
        将输入的单词首字母变成大写并输出。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDOJ