统计一行字符中单词的个数

    技术2024-10-07  55

    C语言统计一行字符中单词的个数,每个单词之间用空格分开

    #include <stdio.h> int main() { char str[100]; int count; fgets(str, sizeof(str), stdin);//从标准输入中读取数据 count = (str[0] != ' ') ? 1 : 0;//如果第一个字符非空,则count起始值为1 for (int i = 1; str[i] != '\0'; i++) { if ((str[i] == ' ') ? 1 : 0)//如果字符为空格,则if内语句值为1,if为真,count自增 count++; } printf("%d", count); return 0; }

    (顺手点个赞再走?虽然不知道赞有啥用。。)

    Processed: 0.011, SQL: 9