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
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())!='\n') if(ch==' ') 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!