The code should look like this:
But doesn't work!Code:for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
else
{
printf(" ");
}
}
}
Printable View
The code should look like this:
But doesn't work!Code:for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
else
{
printf(" ");
}
}
}
What happens if the letter isn't 'a', it's 'e'? Your if( this is a ) check fails, so you else print a space ... then you keep checking, and you see it's an e, and you print a *, then you keep checking, and you see it's not an i, so you print a space ...
This is why I suggested wrapping the inner for loop in a function that tells you if you've placed a vowel or not. Otherwise, you're going to still have to keep track of if you've successfully placed a * or not some other way.
Quzah.
You only want to print a space if you know its a vowel. So the inner for loop is basically checking if its a vowel and if so, then printing a "*". In addition to printing some character, it should set a flag so that when youre outside of the inner for loop, you check this flag--if its true, it means a vowel was found so you dont print a space, otherwise a vowel was not found so you have to print a space. Since this flag must be visible within the inner for loop and within the outer for loop, it makes sense to declare this flag in the (start of the) outer for loop.
The crucial thing is some sort of "flag", saying "yes I did something (found vowel), so you dont have to do the other case (not vowel). As mentioned, you can achieve this by using a function (which has a for loop) and its return value is this implicit flag, or you directly use two for loops with an explicit flag.
Maybe I forgot to mention the flag thing earlier, but that goes back to the fact that giving exact/complete answers/solutions isnt a good thing, you can try and figure out the last missing detail.
You just need to work down through each step as you actually think them. Don't assume that thoughts like 'found a vowel' don't need their own step and that they're magically going to resolve themselves in your code. You need to actually think and translate each logical step, no mater how small it is, into some related action.Code:for...
/* check for vowel */
for ...
if found a matching vowel
yes we found a vowel
/* outside of vowel check */
did we find a vowel in there? if so, what does that mean for what we need to do?
Quzah.
Simplify: write a program that reads in a character and prints "vowel" if the character is a vowel, and "not vowel" otherwise.Quote:
Originally Posted by georgio777
A few of us have given a number of hints already.Quote:
At least give a hint.
You can figure it out from my previous pseudocodeYou can see that in the inner for loop theres an "if", and outside the inner for loop theres an "else". The only way this can be achieved is with some flag, i.e.Code:for each character in string
for each vowel
if the string's character matches a vowel, its (obviously) a vowel and you can stop/break this for loop
end for
otherwise it is not a vowel
end for
Code:for each character in string
flag = false;
for each vowel
if the string's character matches a vowel, its (obviously) a vowel and you can stop/break this for loop, set flag true
end for
if flag is false it is not a vowel so print space
end for
OK, I'm done with this, probably I'm going to talk directly with my teacher.
This is so confusing :confused:
Thank you anyway to everyone.
At least you gave me an idea!
Have a nice day and happy programming!
You giving up is just as frustrating for you as it is for us after devoting our time (at least I feel so).
The second piece of pseudocode in my previous post is pretty much the (one of the many) solution(s). Its a very slight modification of what you already have, just moving a thing or two, and adding a new variable and if statement.