fork download
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int vowel_count(char my_sen[])
  6. {
  7. int wcount = 0; // number of words with 3+ vowel chars
  8. int vcount = 0; // current number of vowel chars in the current word
  9. int i = 0; // index into the string
  10. int ch;
  11. while ((ch = my_sen[i++]) != '\0')
  12. {
  13. if (isspace(ch) || !isalpha(ch))
  14. {
  15. // ch is not an alphabetical char, which can happen either
  16. // before a word or after a word.
  17. // If it's after a word, the running vowel count can be >= 3
  18. // and we need to count this word in.
  19. wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
  20. vcount = 0; // reset the running vowel counter
  21. continue; // skip spaces and non-alphabetical chars
  22. }
  23. if (strchr("aeiouAEIOU", ch) != NULL) // if ch is one of these
  24. {
  25. ++vcount; // count vowels
  26. }
  27. }
  28. // If my_sen[] ends with an alphabetical char,
  29. // which belongs to the last word, we haven't yet
  30. // had a chance to process its vcount. We only
  31. // do that in the above code when seeing a non-
  32. // alphabetical char following a word, but the
  33. // loop body doesn't execute for the final ch='\0'.
  34. wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
  35. return wcount;
  36. }
  37.  
  38. int main(void)
  39. {
  40. char sen[] = "CONSTITUTION: We the People of the United States...";
  41. printf("# of words with 3+ vowels in \"%s\" is %d", sen, vowel_count(sen));
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
# of words with 3+ vowels in "CONSTITUTION: We the People of the United States..." is 3