Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a c++ program that finds the number of vowels used in an string

Tags:

c++

Write a c++ program that finds the number of vowels used in an string.

For the above problem I written a program as follows:

int main()
{   
    char x[10];
    int n,i,s=0;
    cout<<"Enter any string\n";
    cin>>x;
    n=strlen(x);
    for(i=0;i<n;++i)
    {
        if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
        {
            s=s+1;
       }
    }
    cout<<s;
    return 0;
}

Output of the program is as:

Enter any string
elephant
3

Here in 'elephant' at three places vowels are used but the total number of vowels used is 2(e and a) not 3

I am asking to improve the program so that it counts the total number of vowels and print the total number.(e.g. in case of elephant it must give 2)

like image 725
Singh Avatar asked Nov 21 '25 18:11

Singh


2 Answers

Make another array(), with 5 index, like

 vowels[5] = array(0,0,0,0,0);

Then make if else if, with eache vowel, and add

 if(x[i] == 'a') vowels[0] =1;
 elseIf(x[i] == 'e') vowels[1] =1;

etc, and then check if vowels array is set to 1 or 0, and count only, these which are 5.

int count=0;
foreach vowels as item {
  if(item == 1) count++

}
return count;
like image 170
Michael Avatar answered Nov 24 '25 08:11

Michael


The easiest solution would be to just insert each vowel you see into an std::set, and use its size function when you're done.

And for heaven's sake, use a table lookup to determine whether something is a vowel (and put the logic in a separate function, so you can correct it when you need to handle the "sometimes y" part).

Alternatively, without using the standard algorithms:

int charCount[UCHAR_MAX + 1];

//  and for each character:

++ charCount[static_cast<unsigned char>( ch )];

(Of course, if you're using C++, you'll read the characters into an std::string, and iterate over that, rather than having an almost guaranteed buffer overflow.)

Then, just look at each of the vowels in the table, and count those which have non-zero counts:

int results = 0;
std::string vowels( "aeiou" );  //  Handling the sometimes "y" is left as an exercise for the reader.
for ( auto current = vowels.begin(); current != vowels.end(); ++ current ) {
    if ( charCount[static_cast<unsigned char>( *current )] != 0 ) {
        ++ results;
    }
}

Of course, neither of these, implemented naïvely, will handle upper and lower case correctly (where 'E' and 'e' are the same vowel); using tolower( static_cast<unsigned char>( ch ) ) will solve that.

EDIT:

Since others are proposing solutions (which are only partially correct):

bool
isVowel( unsigned char ch )
{
    static std::set<int> const vowels{ 'a', 'e', 'i', 'o', 'u' };
    return vowels.find( tolower( ch ) ) != vowels.end();
}

int
main()
{
    std::string text;
    std::cout << "Enter any word:";
    std::cin >> text;
    std::set<unsigned char> vowelsPresent;
    for ( unsigned char ch: text ) {
        if ( isVowel( ch ) ) {
            vowelsPresent.insert( tolower( ch ) );
        }
    }
    std::cout << vowelsPresent.size() << std::endl;
}

Separating the definition of a vowel into a separate function is practically essential in well written code, and at the very least, you need to mask differences in case. (This code also punts on the question of "y", which would make isVowel several orders of magnitude more difficult. It also ignores characters outside of the basic character set, so "naïve" will report two different vowels.)

like image 20
James Kanze Avatar answered Nov 24 '25 08:11

James Kanze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!