Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language statistics word count
C language statistics word count
Q: Enter a string and output how many words there are. ?

Good wishes?

A:

# include & lt stdio.h & gt # include & lt string.h & gt # Define the size of 20int main ().

{ char str[SIZE]= { ' \ 0 ' }; int count = 0;

printf(" plz input string \ n ");

gets(str);

Sell (str); int length = strlen(str); for(int I = 0; I < length; i++)

{

if(str[i]! =' ')

{ count++; while(str[i]! =“”& amp; & ampstr[i]! ='\0')

{

i++;

}

}

}

printf("%d\n ",count); Returns 0;

}

What's the difference between "\ 0" and "0"?

In C language, they are all characters, and they are all stored in the corresponding ASCII code. For example, the first ASCII code, 0, corresponds to (Null), that is,' \ 0', that is, empty characters. In C language, the sign to judge whether a string ends is to see whether it conforms to' \ 0', and if it conforms to' \ 0', it means that the string ends. The difference between the character' 0' and the number 0: the former is a character constant and the latter is an integer constant, but the character constant can participate in related operations in the program like an integer.

Expand all characters of the data string. If the current character is not empty, the number of words is+1, and then a while loop is nested to judge whether the current word ends.

# include & ltstdio.h & gt?

# include & ltstring.h & gt?

int count_words(char* s)?

{

int len = strlen(s); ?

int count,I;

for(I = 0; Me & ltleni++)?

{

if(*(s+i)! =' '){ ? //If the current code is not empty

count++; ? //Words+1

while(*(s+i)! =“”& amp; & I & ltlen)? //Judge whether the current word ends.

i++;

}

}

Returns the count;

}

int main()

{

Char* a= "I love you";

printf("%d ",count _ words(a));

}