Home >Backend Development >C#.Net Tutorial >How to count the number of words in C language

How to count the number of words in C language

angryTom
angryTomOriginal
2020-03-09 13:41:1413556browse

How to count the number of words in C language

How to count the number of words in C language

The program does not recognize words, but the words in the article are separated by spaces. In other words, number of words = number of spaces 1.

All, the number of words counted in C language is actually converted into the number of spaces in the statistical article.

With this way of thinking about changing the problem, the whole problem will be much simpler. You can implement it yourself according to this idea first, or you can directly look at the code below to implement it.

Recommended learning: c language video tutorial

#include <stdio.h>
int main()
{
    printf("输入一行字符:\n");
    char ch;
    int i,count=0,word=0;
    while((ch=getchar())!=&#39;\n&#39;)
        if(ch==&#39; &#39;)
            word=0;
        else if(word==0)
        {
            word=1;
            count++;
        }
    printf("总共有 %d 个单词\n",count);
    return 0;
}

Program test:

输入一行字符:
I Love China
总共有 3 个单词

输入一行字符:
I Love Xichang College
总共有 4 个单词

More C languageIntroduction to programming tutorial, please pay attention to PHP Chinese website!

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

Statement:
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