# Thread: Function with array to find number of vowels and consonants

1. The code should look like this:

Code:
```	for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
else
{
printf(" ");
}
}
}```
But doesn't work!

2. 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.

3. Originally Posted by quzah
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.
Oh, now I understand!

Why this one is not working also?

Code:
```for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
break;
}
printf(" ");
}```

4. Originally Posted by georgio777
The code should look like this:

Code:
```    for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
else
{
printf(" ");
}
}
}```
But doesn't work!
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.

5. 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.

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.
Yep, I understand reason why is not working and why is wrong, but how about the one you told me?

This one:

Code:
```for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
break;
}
printf(" ");
}```

7. 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.

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.
Well, that's what I am trying to do, but now I'm getting more and more lost

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.
At least give a hint. I don't really want to spent all day long trying to find the solution.

Another thing, I never heard of the definition flag or explicit flags, what does this means.

10. 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?```
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.

Quzah.

11. Originally Posted by georgio777
Well, that's what I am trying to do, but now I'm getting more and more lost
Simplify: write a program that reads in a character and prints "vowel" if the character is a vowel, and "not vowel" otherwise.

12. At least give a hint.
A few of us have given a number of hints already.

You can figure it out from my previous pseudocode
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```
You 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
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```

13. OK, I'm done with this, probably I'm going to talk directly with my teacher.

This is so confusing

Thank you anyway to everyone.

At least you gave me an idea!

Have a nice day and happy programming!

14. 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.