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