Counting vowels program creating loop

ok so I'm not looking for anybody to write code for me, but I am rally confused with this and wonder if anybody can point me in the right direction, I have to write a program that takes a sentence counts the vowels per word then outputs to a file the words that have more than three vowels, I'm pretty sure I know how to do everything except, I have never made a program to count char, how do I make a loop that does one word then goes onto the next, but knows to stop at a .? Thanks in advance if anybody can help
Ok, so you can read a line of text into a string using getline().

Once you have the string, you can either use the find() method or write your own loop to look for spaces/punctuation that would end a word. You can use s[x] to access individual characters of the string s (where x is the index, 0-based).

Keep two variables -- a count of the number of vowels you've seen and a string which is initially blank.

As you read each character of the sentence, if the character is non-blank, add the character to your string variable. Also, if the character is a vowel, add 1 to your counter.

Once you hit the end of the word, check to see if your count is at least 3. If it is, write your string variable to the file. Then reset the counter to zero and reset the string variable to blank.

Continue doing this until you've reached the end of the sentence.



Something like this pseudo-code:
1
2
3
4
5
6
7
8
9
10
11
12
  read_sentence_into_string_s;
  string word;
  int count;
  for( index = 0; index < length-of-s; ++index )
      if( s[ index ] is non-blank )
         add s[ index ] to end of word;
         if( s[ index ] is a vowel )
            add 1 to count;
      else
         if( count is at least 3 )
             write word to file;
         reset count and word
this is my first foray into strings and I already hate them, I have no idea what is going on, I was just getting used to loops dammit!
I'd approach this very simply.

1
2
3
4
5
6
7
8
9
10
11
12
string var = "some sentence with many vowels in it";
int vowels = 0;

for (int i = 0; i < var.length(); ++i) {
 char currentLetter = toUpper(var[i]);
 switch(currentLetter) {
  case 'A':
  case 'E': // etc
   vowels++;
   break;
 }
}


Think of that as pseudo-code.
Last edited on
i get this, but that is just going to count the vowels in the entire string, I am trying to get it to count vowels PER WORD, this is what I cant figure out how to do, how do I get the original sting which is the entire sentence, then get the loop to go for one word, run the function on that word, then go to the next word and try the same thing, and to make matters worse, it has to be 3 different vowels per word to get a good result
Yes you are right, It will do the whole string.

So now, it's upto you to figure out how to split that string up into individual words.

Have a look at string.find(); /* and */ string.substring(); and it should be a piece of cake.
how do you use the substring method?
i tried string2 = string1.substring(' ', ' ') but I just get a error, I cant figure out how to make the substring a word, I've spent about 6 hours trying to figure this out, my manual really doesnt give any examples of this
http://www.cplusplus.com/reference/string/string/substr.html

I hoop that helps. This site is pretty usefull :)
yeah, i've been reading that for about the last half hour, I have no idea what I am going wrong, I think I just going to give up on this one before I throw my computer against the wall
ok lets forget the fact that I havent even started figuring out how I am going to get it to read each word but if I use this code this is all I get as a output is

esting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main ()
{
string sentence ="This is the sentence that I am testing";

string str2;

	str2 = sentence.substr(' ',' ');
cout << str2 << endl;
return 0;
}



To count the vowels in a word, you can use this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int numberofvowels=0;
    string vowel[6]={"a","e","i","o","u","y"};
    string input;
    cin>>input;
    for (int i=1;i<=input.length();i++)
    {
        string thischar=input.substr(i,1);
        for (int j=0;j<=5;j++)
        {
            if (thischar==vowel[j])
            numberofvowels++;
        }
    }
    cout<<numberofvowels;
    cin.ignore();
    return 0;
}


To seperate a string into words you can use this code:

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str="Hello World!";
    string word[100];
    int wordcounter=1;
    for (int i=0;i<=(str.length()-1);i++)
    {
    string thischar=str.substr(i,1);
    if (thischar==" ")
    {
       wordcounter++;
    }
    else
    {
       word[wordcounter]+=thischar;
    }
    }
    for (int i=1;i<=wordcounter;i++)
    cout<<word[i]<<endl;
    cin.ignore();
    return 0;
    
}


the code for the vowels is not a problem, i need to find 3 different vowels in a word, i am going to use a switch and if it see the vowel assign a 1, not a +1, that way at the end of the word I can see if it is more than three or not. as for the splitting words since I want to know what is going on, why is string word[100] what is the 100 for?
I created an array of 100 words to store the string in. I didnt know how long the string in your program would be, so i picked a big number to be sure. The code stores the current word in word[index]. So this code can seperate strings up to 100 words (after that the program will start acting strange).

This isnt the most efficient way to do this: it copys the word letter by letter to word[index]. You may want to use another way, i just wanted to show you one way to do this.
Last edited on
maybe my code will help you figure out what i am trying to do, it doesnt work but im getting to the point where i dont care about it anymore
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
45
46
47
48
49
50
51

#include <iostream>
#include <string>

using namespace std;
int vowelcheck (char);
int main()
{
    
	int vowela, voweli, vowele, vowelo, vowelu, numblet;
	string str="This is the computer that I am trying to figure out how to build this aei.";
    string word[100];
    int wordcounter=1;
    for (int i=0;i<=(str.length()-1);i++)
    {
		string thischar=str.substr(i,1);
		if (thischar==" ")
		{
			wordcounter++;
		}
		else
		{
			word[wordcounter]+=thischar;
		}
    }
    for (int i=1;i<=wordcounter;i++)
	{
		numblet = int(word[i].length());
		for (int j=0; j < numblet; j++)
		{
		vowela = 0;
		vowele = 0;
		voweli = 0;
		vowelo = 0;
		vowelu = 0;
			switch (word[i].at(i))
		{case 'a': vowela = 1;
			case 'e': vowele = 1;
			case 'i': voweli = 1;
			case 'o': vowelo = 1;
			case 'u': vowelu = 1;
		}
		}
		if (vowela + vowele + vowelo + voweli + vowelu >= 3)
			cout << word[i] << endl;
	}
		return 0;
    
}


Here is your code:

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
45
#include <iostream>
#include <string>

using namespace std;
int vowelcheck (char);
int main()
{
    
    string str="This is the computer that I am trying to figure out how to    
    build this aei.";
    string word[100];
    int wordcounter=1;
    for (int i=0;i<=(str.length()-1);i++)
    {
		string thischar=str.substr(i,1);
		if (thischar==" ")
		{
			wordcounter++;
		}
		else
		{
			word[wordcounter]+=thischar;
		}
    }
    for (int i=1;i<=wordcounter;i++)
	{
		numbervowels=0;
        numblet = int(word[i].length());
		for (int j=0; j < numblet; j++)
		{
		string vowel[6]={"a","e","i","o","u"};
		string thischar=word[i].substr(j,1);
		for (int n=0;n<=4;n++)
		{
            if (thischar==vowel[n])
            numbervowels++;
        }
		}
		if (numbervowels>= 3)
			cout << word[i] << endl;
	}
		cin.ignore();
        return 0;
    
}


I'm using the same methods as in the two codes i posted before.
I hope you understand how it works, if not: please ask (i will answer them tomorrow, i must go now).
Last edited on
Topic archived. No new replies allowed.