Displaying Number of Words Not Containing Certain Letters

Hello,

I am trying to fix a code that does various statistics on an imported txt file (words are imported and stored in a string). and one those stats that i am having a trouble to get it working is to count the number of words in the txt file that does not contain the letters (a,e,i,o,u or y).

Here is the part of my code that i having a trouble with:

1
2
3
4
5
6
7
8
9
while (!wordfile.eof())
{
for ( int k = 0; k < word.length(); k++)
{
if (word [k] != 'a' && word [k] != 'e' && word [k] != 'i' && word [k] != 'o' && word [k] != 'u' && word [k] != 'y')
      noVowels++;
}
wordfile.close();


The problem here is that it is displaying the number of letters that are not (a,e,i,o,u,y), while i am trying to make it display the number of words not containing those letters.

Here is another part of the code i am working on that displays the number of words starting with "a", and is working:

1
2
if ( word [0] == 'a' ) 
      startA++;



Any pointers will be most appreciated.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
int no_of_words_without_vowels = 0;

for each word encountered // <-- this is pseudocode
{
   get next word until there aren't any more // <-- this is pseudocode too ;)
   
   for ( int k = 0; k < word.length(); k++)
   {
      if (word [k] != 'a' && word [k] != 'e' && word [k] != 'i' && word [k] != 'o' && word [k] != 'u' && word [k] != 'y')
      no_of_words_without_vowels++;
    }
} 
Last edited on
Be careful when using the operator!= with the operator&&. If word[k] is equal to 'a' then word[k] will be not equal to 'e'.

Also have you considered using one of the string search functions, ie find_first_of(), find_first_not_of(), etc.?

is word a std::string?

you could use std::find

http://www.cplusplus.com/reference/string/string/find/

1
2
3
4
5
6
7
bool founda = word.find("a") == std::string::npos ;
bool founde = word.find("e") == std::string::npos ;
bool foundi = word.find("i") == std::string::npos ;
bool foundo = word.find("o") == std::string::npos ;
bool foundu = word.find("u") == std::string::npos ;

noVowels += (founda && founde && foundi && foundo && foundu) ? 1 : 0;
Here is a way to find the words without vovels.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

bool containsVowels(const string& word)
{
  for (const char& ch : word)
  {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' 
        || ch == 'u' || ch == 'y')
    {
      return true;
    }
  }
  return false;
}

int main ()
{
  ifstream src ("words.txt");
  size_t noVowels = 0;

  if (!src)
  {
    perror ("File error: ");
    return EXIT_FAILURE;
  }
  string word;
  while (src >> word)
  {
    if (!containsVowels(word) )
    {
      noVowels++;
      cout << word << "\n";
    }
  }
  system ("pause");
  return 0;
}
Or perhaps:

1
2
3
4
bool containsVowels(const string& word)
{
    return word.find_first_of("aeiouyAEIOUY") != std::string::npos;
}


Topic archived. No new replies allowed.