1. Home > programming >

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, the number of words = the number of spaces +1.

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

With this kind of problem transformation, the whole problem is much simpler. You can implement it yourself according to this idea first, or you can directly look at the following code implementation.

Recommended learning: c language video tutorial

#include  int main() {printf("Enter a line of characters  :\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("A total of %d words\n",count); return 0; }
< p>Program test:

Enter a line of characters: I Love China A total of 3 words Enter a line of characters: I Love Xichang College A total of 4  Words

For more introductory tutorials on C language programming, please pay attention!

The above is the detailed content of the method of counting words in C language. For more, please pay attention to other related articles!

Content recommendation: free high-definition PNG material download

This article is published by www.internetweblist.com and does not represent the position of www.internetweblist.com/:http://www.internetweblist.com/programming/52509.html

Contact Us

Online consultation:click here to give a message