fork download
  1. #include <stdio.h>
  2.  
  3. void strip_string(char* str, int max_len)
  4. {
  5. int count = 0;
  6. while(*str++ != '\0' && count++ < max_len);
  7.  
  8. if(count > 1) {
  9. str -= 2;
  10. if(*str == '\n')
  11. *str = '\0';
  12. }
  13. }
  14.  
  15. int main(void)
  16. {
  17. char str[100];
  18. fgets(str, sizeof(str), stdin);
  19. strip_string(str, sizeof(str));
  20.  
  21. int i = 0, word_count = 0, var_flag = 0;
  22.  
  23. while(str[i] != '\0'){
  24.  
  25. if (str[i] == 32 && var_flag != 0) {
  26. var_flag = 0;
  27. }
  28.  
  29. if (str[i] != 32 && var_flag != 1) {
  30. var_flag = 1;
  31. word_count++;
  32. }
  33.  
  34. i++;
  35. }
  36.  
  37. printf("%d", word_count);
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5320KB
stdin
  I love   C  
stdout
3