10 Replies - 7188 Views - Last Post: 14 October 2011 - 05:53 AM Rate Topic: -----

#1 ridgehype23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-September 11

Must count the number of vowels in a string.

Posted 12 October 2011 - 11:26 PM

Using the function int count_string(string str) write a program that counts the number of vowels in tat string.

This is the code I have:

#include <iostream>
#include <string>

using namespace std;

int vowels = 0;

int count_vowel(string str)
{
    
    if (str == "a") vowels++;
    if (str == "e") vowels++;
    if (str == "i") vowels++;
    if (str == "o") vowels++;
    if (str == "u") vowels++;
    if (str == "A") vowels++;
    if (str == "E") vowels++;
    if (str == "I") vowels++;
    if (str == "O") vowels++;
    if (str == "U") vowels++;
    return 0;
}


int main(void) { 
    string input; 
    while ( true ) { 
        cout << "Enter a string or Q to quit: "; 
        getline( cin, input );
        count_vowel ( input);
        if ( input == "Q" ) 
            break;
        cout << " This string has " << vowels << " vowels!";
    } 
    return 0; 
}


I don't know how to make it count the number of vowels.

Is This A Good Question/Topic? 0
  • +

Replies To: Must count the number of vowels in a string.

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Must count the number of vowels in a string.

Posted 12 October 2011 - 11:36 PM

What about you remove each instance of the vowel and them count the difference in characters between the original string and the edited string

J-Bo
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:36 AM

Quote

I don't know how to make it count the number of vowels.

In your count_vowel() function you are testing if the vowel is equal to the string, not if the vowel is contained in the string. You will need to check the string character by character to see if that character is a vowel.

Also you should stop using the global variable. Use your return statement to return the number of vowels that are contained in the string.

Jim

This post has been edited by jimblumberg: 13 October 2011 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 ridgehype23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-September 11

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 05:09 PM

This is the new code I have

#include <iostream>
#include <string>

using namespace std;

int vowels = 0;

int count_vowel(string str)
{
    
    if (str = "a") return vowels++;
    if (str = "e") return vowels++;
    if (str = "i") return vowels++;
    if (str = "o") return vowels++;
    if (str = "u") return vowels++;
    if (str = "A") return vowels++;
    if (str = "E") return vowels++;
    if (str = "I") return vowels++;
    if (str = "O") return vowels++;
    if (str = "U") return vowels++;
    return 0;
}


int main(void) { 
    string input; 
    while ( true ) { 
        cout << "Enter a string or Q to quit: "; 
        getline( cin, input );
        count_vowel ( input);
        if ( input == "Q" ) 
            break;
        cout << " This string has " << vowels << " vowels!";
    } 
    return 0; 
}

Was This Post Helpful? 0
  • +
  • -

#5 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:22 PM

First you are not counting specific characters in the string. You are check if the whole string is equal to that specific vowel.

If the input string is "test" you are asking is "test"==a?

Secondly inside the conditional of your if statement you are assigning that specific value to that variable, which is always true. You need double "==" like in the first iteration of your code.

strings are arrays of characters. You need to individually access each element in the char array and check if it is a vowel. You are familiar with arrays right?

You need to do this in a loop.

This post has been edited by Ap0C552: 13 October 2011 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 ridgehype23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-September 11

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:29 PM

So use the double equal signs? and no Im not, but I can do some research on them.

View PostAp0C552, on 13 October 2011 - 06:22 PM, said:

First you are not counting specific characters in the string. You are check if the whole string is equal to that specific vowel.

If the input string is "test" you are asking is "test"==a?

Secondly inside the conditional of your if statement you are assigning that specific value to that variable, which is always true. You need double "==" like in the first iteration of your code.

strings are arrays of characters. You need to individually access each element in the char array and check if it is a vowel. You are familiar with arrays right?

You need to do this in a loop.

Was This Post Helpful? 0
  • +
  • -

#7 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:37 PM

yes use double ==




//here is a char names str

char str;

//here is a string named str denoted by []

char str[]="test";

//not String str






t is at position 0 in the array
e is at position 1 in the array
s is at position 2 in the array
t is at position 3 in the array

so to check if the first letter is a vowel we would go


if(str[0]=='A')vowels++;
if(str[0]=='E')vowels++;
if(str[0]=='I')vowels++;
if(str[0]=='O')vowels++;
if(str[0]=='U')vowels++;
if(str[0]=='a')vowels++;
if(str[0]=='e')vowels++;
if(str[0]=='i')vowels++;
if(str[0]=='o')vowels++;
if(str[0]=='u')vowels++;



This post has been edited by Ap0C552: 13 October 2011 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#8 ridgehype23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-September 11

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:40 PM

How would you check all of the letters?
Was This Post Helpful? 0
  • +
  • -

#9 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:45 PM

Using a "for loop", are you familiar with loops?

Have you been skipping class? LOL

This post has been edited by Ap0C552: 13 October 2011 - 06:45 PM

Was This Post Helpful? 0
  • +
  • -

#10 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Must count the number of vowels in a string.

Posted 14 October 2011 - 05:48 AM

My FOR loops tutorial

Try that and see if it helps.

A character array and conditional loop is the most simple solution to this problem, so it's probably your best bet.

Hope this helped!
Was This Post Helpful? 0
  • +
  • -

#11 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Must count the number of vowels in a string.

Posted 14 October 2011 - 05:53 AM

EDIT: Also note that your count_vowels function always returns 0 because you hard coded it like that.

It should return vowels and vowels should be in the local scope of the function, otherwise if you tested two different words it would count the vowels in both instead of resetting to 0.
	 
int count_vowel(string str)
{
    int vowels = 0;
    
    //for(the loop to check if there is a vowel)
    //{
    //    if(the scanned letter is a vowel)
    //    {
    //        vowels++;
    //    }
    //}
    return vowels;
}



It's a simple fix, but it should save you a minor headache in debugging.

Hope this helped, too! :D

This post has been edited by Gungnir: 14 October 2011 - 06:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1