-1

I'm trying to solve a problem where based on a k number which is the minimum number of vowels will print all the words with at least k vowels. So, for example having:

Input: with k=2
  cars are...really nice

Output: 
 are
 really
 nice

I think i'm pretty close but i'm having a few issues with saving the words and than checking the number of vowels. Here is the code:

int is_letter(char c) {
  if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
    return 1;
  return 0;
}


int main(){


    ifstream fin("dateTest.in");
    char s[250];
    char word[250];
    int k;
    fin>>k;
    int nrVowels=0;

    while(fin>>s){
         int n = strlen(s);
         int have_word = 0;

          for (int i = 0; i < n; ++i){
            if (is_letter(s[i])){
              have_word = 1;
              word[i]+=s[i]; //Here i'm having some problems

            }else if (have_word == 1) {
              for(int i=0; i<strlen(word); i++){
                 if (word[i] == 'a' ||  word[i] == 'e' ||  word[i] == 'o' ||  word[i] == 'i' || word[i] == 'u'){
                        nrVowels++;

                        if(nrVowels >= k){
                            cout<<word;
                        }
              }
            }
             have_word = 0;
             //Here i should somehow reset the word variable?
        }
      }
    }
}

At first i created a function that checks if a character is a letter. After that i have a while which iterates through all the words from a file, a for which verifies every word and if it is a word, that word should be saved in another variable which i'm doing wrong:( and if it's not finding another word it means that the word ended and it should check for vowels and then print the word if it has a minimum of k vowels. My problem is that the way i save the word it's not working and at the end i should somehow reset the word variable so that another word will be saved there.

Tr Sorin
  • 125
  • 9
  • I'm not sure why you're trying to copy the word from 's' into 'word'. At all times 's' contains the text you are interested in. You can look for vowels in that array rather than in 'word'. Likewise, you can output 's'. Don't forget to 'break' out of the for loop after printing the word. – Tim Randall Jul 27 '18 at 20:33

1 Answers1

2

I would store the words in a map with the string being the number of vowels being the data.

Then it would be easy to iterate over the map printing all words with data value less or equal to your k.


The first part could be done by reading word by word (just use the normal formatted input operator >> to read into a string), and count the vowels for that word. Then insert the string-vovel count pair into the map.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621