C Language Implementation statistics number of words

Source: Internet
Author: User

Programming Implementation, enter a line of characters from the keyboard to count the number of words in it.
Where: Words are separated by a space, and the number of spaces is at least one.
Requirement: Array type is character type
Use scanf to enter a line of characters.
Output: The number of words.
Focus: The maximum number of characters in a line is 80, defining a one-dimensional array to hold these characters,
When a space is encountered, continue to see if the next non-whitespace character, if so, the number of words plus 1,

Otherwise, continue reading the characters until the end.



Note:

The last step of the output of the variable i output is count++, because I did not calculate the first word at the time of calculation,

So add the first word. So the output number of time will be output count++;

Code:


#include <stdio.h>
#include <string.h>
#define MAXN 100
Char STR[MAXN];
int main ()
{
int len = 0, count = 0, I;
Gets (str);
len = strlen (str);
printf ("The length entered is:%d\n", Len);
for (i = 1; i < Len; i++)
{

if (Str[i] <= ' z ' && str[i] >= ' a ' | | str[i] <= ' z ' && str[i] >= ' a ')
{
if (str[i-1] = = ")
{
count++;
}
}
}
printf ("Number of words:%d\n", Count + 1);
return 0;
}


C Language Implementation statistics number of words

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.