fork(1) download
  1. /*Procedure that counts the number of words in a string
  2.   A word is any set of characters (or a single character) separated
  3.   from the word after it and before it by a space*
  4.  
  5.   For example, "a" is one word, "!" is one word, "peut-etre" is one word, "Builder Jack" are two words*/
  6.  
  7. #include <stdio.h>
  8.  
  9.  
  10. int word_count(char *array){
  11. int count = 0; /*Number of words*/
  12.  
  13. while(*array != '\0'){
  14. if(*array == ' '){
  15. count++;
  16. }
  17. array++;
  18. }
  19. return (count+1);
  20. }
  21.  
  22. int main(void){
  23. char line[1000];
  24.  
  25. puts("Enter text");
  26.  
  27. fgets(line, sizeof(line), stdin);
  28.  
  29. printf("Word count is %i", word_count(line));
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 2172KB
stdin
Standard input is empty
stdout
Enter text
Word count is 2