#include %26lt;stdio.h%26gt;
#include %26lt;strings.h%26gt;
int
main(int argc, char* argv[])
{
char *token, *string = argv[1];
int count = 0;
while ((token=strtok(string, "\t ")) != 0) {
count++;
string = 0;
}
printf("String has %d words\n", count);
return 0;
}
And to show that it works:
$ cc cw.c
$ a.out "this is a simple test"
String has 5 words
Write a C function that counts the number of words in a string.?
if you're doing this for a class, please learn it and don't ask someone else to do it for you. If you're not taking a class and you just need help with part of a program you're writing then the following information should be enough to help you out:
-use a tokenizer to determine where a word begins and ends
-use a counter within a loop that increases by one every time a new word is found.
Reply:See link
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment