Finish this for me, cpp int countValidWords(char* s) { int totalWords = 0, bufferIndex =…
Private research annotation
Sign in to add a private research label or note.
- User
Finish this for me, cpp
int countValidWords(char* s) { int totalWords = 0, bufferIndex = 0; char* buffer = malloc(strlen(s) * sizeof(char)); for(int i = 0; i < strlen(s); i++) { // Go until we find ' ' if(s[i] != ' ') { // Fill buffer buffer[bufferIndex++] = s[i]; } else { // Evaluate // At least 3 chars // Contains a vowel and a consonant bool containsVowelAndConsonant = false;; for(int j = 0; j < bufferIndex; j++) { char c = buffer[j]; containsVowelAndConsonant = containsVowelAndConsonant || (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // containsVowelAndConsonant = containsVowelAndConsonant || (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } if(bufferIndex >= 3 && containsVowelAndConsonant) { // We got a word! totalWords++; } } } // bonus points free(buffer); return totalWords; }
- Assistant