题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2026
首字母变大写
算法分析:第一个字母变为大写。其余,查找空格后的第一个字母将其变为大写后输出。
其中要注意的是gets函数的返回值。
对于gets函数:
读入成功,返回与参数buffer相同的指针;读入过程中遇到EOF(End-of-File)或发生错误,返回NULL指针。所以判断读取文章末尾为gets(a)==null
我的ac代码:
#include <stdio.h> int main() { char a[100]; while (gets(a)!=NULL) { a[0]=a[0]-32; for(int i=1;a[i]!='\0';i++){ if(a[i]==' '){ a[i+1]=a[i+1]-32; } } for(int i=0;a[i]!='\0';i++){ printf("%c",a[i]); } printf("\n"); } return 0; }