c语言统计字符串中包含空格,数字的个数。如何在后面显示录入内容?

2025-03-04 03:59:18
推荐回答(2个)
回答1:

#include 
int main(){
        int space=0,number=0,others=0;
        char str[1000];
        char nextchar = 0;
        printf("输入任意文字:\n");
        scanf("%s", str);
        printf("输入内容:%s\n", str);
        getchar();//ignore enter 
        for(;nextchar!='\n';) {
                scanf("%c",&nextchar);
                if(nextchar==' ')
                        space++;
                else if('0'<=nextchar&&nextchar<='9')
                        number++;
                else
                         others++;
        }
        printf("空格=%d,数字=%d,其他=%d\n",space,number,--others);
}

你这个逻辑就很不对好的么 

而且你如果输入char类型的 就要考虑换行这个字符

少谈恋爱 且行且珍惜

回答2:

你K定义的类型不对,应该是char 型,