不用数组实现单词首字母大写

编写程序,输入一行字符,将每个单词首字母改为大写后输出。所谓单词是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。
输入
输入一行字符。
输出
在一行中输出已输入字符,其中所有单词首字母已改为大写。
How are you?
How Are You?

如下:

img

代码

#include <stdio.h>
int main()
{
    char buf[100];
    int i=0;
    gets(buf);
    while(buf[i] != '\0')
    {
        if(i==0 && buf[i]>='a' && buf[i]<='z')
            buf[i]-=32;
        else if (i>0 && buf[i]>='a' && buf[i]<='z' && buf[i-1]==' ')
            buf[i]-=32;
        printf("%c",buf[i]);
        i++;
    }
    return 0;
}


指针的方式

#include<ctype.h>
#include<string.h>
#include<stdio.h>
void top(char *s)
{
    int i=0;
    for(;*s;s++)
        if(i)
        {
            if(*s==' ')
            i=0;
        }
        else
        {
            if(*s!=' ')
            {
                i=1;
                *s=toupper(*s);
            }
        }
}
main()
{
    char str[81];
    gets(str);
    top(str);
    printf("%s\n",str);
}