C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-16-2009, 09:22 PM   #31
Registered User
 
georgio777's Avatar
 
Join Date: Sep 2009
Posts: 70
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!
georgio777 is offline   Reply With Quote
Old 11-16-2009, 09:23 PM   #32
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 11-16-2009, 09:28 PM   #33
Registered User
 
georgio777's Avatar
 
Join Date: Sep 2009
Posts: 70
Quote:
Originally Posted by quzah View Post
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!

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   Reply With Quote
Old 11-16-2009, 09:29 PM   #34
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 1,230
Quote:
Originally Posted by georgio777 View Post
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.
nadroj is offline   Reply With Quote
Old 11-16-2009, 09:30 PM   #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   Reply With Quote
Old 11-16-2009, 09:31 PM   #36
Registered User
 
georgio777's Avatar
 
Join Date: Sep 2009
Posts: 70
Quote:
Originally Posted by nadroj View Post
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(" ");
	}
georgio777 is offline   Reply With Quote
Old 11-16-2009, 09:33 PM   #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   Reply With Quote
Old 11-16-2009, 09:34 PM   #38
Registered User
 
georgio777's Avatar
 
Join Date: Sep 2009
Posts: 70
Quote:
Originally Posted by nadroj View Post
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
georgio777 is offline   Reply With Quote
Old 11-16-2009, 09:35 PM   #39
Registered User
 
georgio777's Avatar
 
Join Date: Sep 2009
Posts: 70
Quote:
Originally Posted by nadroj View Post
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.

Last edited by georgio777; 11-16-2009 at 09:37 PM.
georgio777 is offline   Reply With Quote
Old 11-16-2009, 09:36 PM   #40
+++ OK NO CARRIER
 
quzah's Avatar
 
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?
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-16-2009, 09:37 PM   #41
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,362
Quote:
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.
__________________
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   Reply With Quote
Old 11-16-2009, 09:38 PM   #42
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 1,230
Quote:
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
nadroj is offline   Reply With Quote
Old 11-16-2009, 09:43 PM   #43
Registered User
 
georgio777's Avatar
 
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   Reply With Quote
Old 11-16-2009, 09:46 PM   #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   Reply With Quote
Old 11-16-2009, 10:13 PM   #45
Registered User
 
georgio777's Avatar
 
Join Date: Sep 2009
Posts: 70
Quote:
Originally Posted by nadroj View Post
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.
OK, I managed to make it work!!! Thank you for your time, I didn't know it was that easy. Sorry if I insisted to much. I accept I was a little immature. I promise it would happen again!

Again thank you to everyone, especially nadroj!
georgio777 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22