![]() |
| | #31 |
| Registered User Join Date: Sep 2009
Posts: 70
| Code: for(i=0; str[i] != '\0'; i++)
{
for(j=0; strv[j] != '\0'; j++)
{
if (str[i] == strv[j])
{
printf("*");
}
else
{
printf(" ");
}
}
}
|
| georgio777 is offline | |
| | #32 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,643
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #33 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
But what about this? 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(" ");
}
| |
| georgio777 is offline | |
| | #34 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,230
| 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. |
| nadroj is offline | |
| | #35 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,230
| 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. |
| nadroj is offline | |
| | #36 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
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(" ");
}
| |
| georgio777 is offline | |
| | #37 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,230
| 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. |
| nadroj is offline | |
| | #38 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
| |
| georgio777 is offline | |
| | #39 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
Another thing, I never heard of the definition flag or explicit flags, what does this means. Last edited by georgio777; 11-16-2009 at 09:37 PM. | |
| georgio777 is offline | |
| | #40 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,643
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #41 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,362
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #42 | |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,230
| Quote:
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
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
| |
| nadroj is offline | |
| | #43 |
| Registered User Join Date: Sep 2009
Posts: 70
| 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! |
| georgio777 is offline | |
| | #44 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,230
| 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. |
| nadroj is offline | |
| | #45 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
Again thank you to everyone, especially nadroj! | |
| georgio777 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Count the number of vowels, consonants, digits etc. | kumar14878 | C Programming | 3 | 05-09-2005 12:34 AM |
| function supposed to strip vowels | MB1 | C++ Programming | 11 | 04-23-2005 05:44 PM |
| Very odd segmentation fault/core dump. | ChristianTool | C Programming | 19 | 04-26-2004 06:38 AM |
| Setting int pointers to one function (not using five functions!) | Marc Sharp | C Programming | 4 | 11-23-2003 07:15 AM |
| Massive Function Problem | Marc Sharp | C Programming | 10 | 11-19-2003 08:49 PM |