I am trying to get this code working for extra credit, but I unfortunately only have 40 minutes until it is due:rolleyes:. I would like to know how to do this program, whether I turn it in or not. The letter "Y" can be considered a vowel if there is not a vowel immediately before or immediately after the "y", otherwise it should be considered a consonant. I have everything working except for the Y being counted as a vowel when it is next to consonants.

#include <iostream>
#include <cctype>
using namespace std;

int Y(char string[], int ctr, int vowel);
int length = 0;
int vowel = 0;

int main()
{
    char string[50] = "";
    cout << "Please enter a string of 50 characters or less: ";
    cin.getline(string, 50);
    length = strlen(string);

    for(int ctr = 0; ctr < length; ctr++)
    {
        string[ctr] = tolower(string[ctr]);
        

        if(string[ctr] == 'a' || string[ctr] == 'e' || string[ctr] == 'i' || string[ctr] == 'o' || string[ctr] == 'u')
        {    
            vowel++;

            if(string[ctr] == 'y')
            {    
                vowel = Y(string, ctr, vowel);
            }
        }

        if(string[ctr] == ' ')
        {
            vowel++;
        }
        
    }
    cout << "\nThe string you entered was: "<< string << endl;
    cout << "You entered " << (length - vowel) << " consonants in that string." << endl;
}

int Y(char string[], int ctr, int vowel)
{
    bool flag1 = false;
    bool flag2 = false;
    if(string[ctr++] == 'a' || string[ctr++] == 'e' || string[ctr++] == 'i' || string[ctr++] == 'o' || string[ctr++] == 'u')
    {
        flag1 = true;
    }
    if(string[ctr--] == 'a' || string[ctr--] == 'e' || string[ctr--] == 'i' || string[ctr--] == 'o' || string[ctr--] == 'u')
    {
        flag2 = true;
    }
    if((flag1 == true) && (flag2 == true))
    {
        vowel--;
    }
    return vowel;
}

>>but I unfortunately only have 40 minutes until it is due
unfortunately for you the time expired 6 hours ago. Lesson learned: don't wait until the last minute to get help.

Well, I had a major project that was due as well as finals all this week, so I did not do the extra credit until everything else was finished. Anyway, I would still like to know how to do it.

here is something just off the top of my head -- untested and uncompiled.

int isvowel(char c)
{
   if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
          return TRUE;
   return FALSE;
}

...
...
if( string[ctr] == 'y' )
{
   if( !isvowel(strin[ctr-1]) && !isvowel(strin[ctr+1]) 
   {
          // y is a vowel
   }
}

your program also needs to verify that (ctr-1) >= 0 && (ctr+1) <= length of the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.