Home > Backend Development > C#.Net Tutorial > How to implement word statistics in C language

How to implement word statistics in C language

angryTom
Release: 2020-03-02 17:51:30
Original
3990 people have browsed it

How to implement word statistics in C language

如何用c语言实现单词统计

输入一串字符串,输出其中有多少个单词。

推荐学习:c语言视频教程

代码如下:

#include <stdio.h>
#include <string.h>
#define SIZE 20
int main(){    
    char str[SIZE]={&#39;\0&#39;};   
    int count=0;
    printf("plz input the string\n");
    gets(str);
    puts(str);   
    int length = strlen(str);  
    for (int i=0;i<length;i++){
        if(str[i]!=&#39; &#39;){         
            count++;       
            while(str[i]!=&#39; &#39;&&str[i]!=&#39;\0&#39;){
                i++;
            }
        }
    }
    printf("%d\n",count); 
    return 0;
}
Copy after login

程序解释:

‘\0’和‘0’的区别: 在c语言中,它们都是字符,都用对应的ASCII码来存储。例如第一个ASCII码,0,对应字符为(Null),就是 ‘\0’,即空字符。我们在c语言中判断一个字符串是否结束的标志就是看是否遇到‘\0’,如果遇到‘\0’,则表示字符串结束。字符‘0’和数字0的区别:前者是字符常量,后者是整形常量,但是字符常量可以像整数一样在程序中参与相关运算。

更多C语言教程,请关注PHP中文网

The above is the detailed content of How to implement word statistics in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template