Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can someone help me finish this code? I'm supposed to replace the vowels that have an uneven number with "BAC".

+2 votes
asked Feb 23 by Theodora (140 points)
#include <iostream>

#include <cstring>

using namespace std;

int main()

{

char s[101],*p;

int ok,i=0;

cin.get(s,101);

p=strtok(s," ");

while(p)

{

for(i=0; i<strlen(p); i++)

if(i%2==1)

if(strchr("aeiouAEIOU",p[i])!=0) {; ok=1;}

p=strtok(NULL," ");

}

if(ok==1) cout<<s;

else cout<<"Nu exista.";

return 0;

}

1 Answer

0 votes
answered Feb 23 by Peter Minarik (101,340 points)

Sorry, I'm not quite sure what you're after when you say

I'm supposed to replace the vowels that have an uneven number with "BAC".

Vowels are (as you correctly used in your code) a, e, i, o, u. But what is this even/uneven number you mean that is associated with a vowel? Is it their index in the alphabet (0- or 1-based)? Is it their (ASCII) character code?

Or maybe you're supposed to replace words that have an uneven number of vowels in them with "BAC", not the vowels themselves? (E.g.: "cow" --> "BAC", because single vowel, or "vitamin" --> "BAC", because 3 vowels, but "learn" --> "learn", as 2, i.e., an even number of vowels.

Your code is checking for vowels at even positions. Is this what you need?

Could you please specify what you're after? Maybe use a few examples as an illustration?

f you're looking for the example as I described in bold, then you are supposed to count the vowels and decide after every word, if you're supposed to print it as it was (p), or print "BAC" instead.

I've added some comments in your code so you'd know where to add the new logic:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char s[101], *p;
    int ok, i = 0;
    cin.get(s, 101);
    p = strtok(s, " ");
    while (p)
    {
        int vowelCount = 0;
        for (i = 0; i < strlen(p); i++)
        {
            // Iterate through all th characters of `p`
            {
                // If any character of p is a vowel, increment `vowelCount`.
            }
        }
        
        // If vowelCount is even, print p, else print "BAC".
        p = strtok(NULL, " ");
    }

    return 0;
}

Good luck!

commented Feb 25 by Theodora (140 points)
Hello! Yes, I am supposed to look for words in uneven positions and if my program finds a vowel on the word it’s going to replace the vowel with “BAC”. For example:
We have the phrase: “Going to the cinema”
‘Going’ and ‘the’ are in uneven positions, and it’s going to show on the screen ‘GBACBACng to thBAC cinema’. Hope this helps you understand the function of the code
commented Feb 27 by Peter Minarik (101,340 points)
Thank you for the clarification. So based on this, I'd suggest the following pseudo-code. You can figure out the right C/C++ code here and share your final code for review, if you'd like:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char s[101];
    char * word;
    int wordIndex = 1;

    cin.get(s, 101);
    word = strtok(s, " ");

    while (word)
    {
        // TODO: if wordIndex is odd
        {
            for (char * ch = word; *ch != '\0'; ch++) // We iterate through all the characters in the word
            {
                // TODO: if `*ch` is a vowel
                {
                    printf("BAC");
                }
                else
                {
                    printf("%c", *ch);
                }
            }
        }
        else
        {
            // TODO: wordIndex is even here, so just print the original word without any changes
        }

        word = strtok(NULL, " ");
        wordIndex++;

        // TODO: if word is not NULL, we have more words to follow...
            // TODO: ... so let's separate them by a single space, which was consumed by cin.get()
    }

    return 0;
}
commented Mar 2 by E (530 points)
@Peter Minarik Your points on the main page 100,540 points lol probably a glitch
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...