Function with array to find number of vowels and consonants

This is a discussion on Function with array to find number of vowels and consonants within the C Programming forums, part of the General Programming Boards category; The code should look like this: Code: for(i=0; str[i] != '

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

'; i++) { for(j=0; strv[j] != '
  1. #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!

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #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(" ");
    	}

  4. #34
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  5. #35
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  6. #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(" ");
    	}

  7. #37
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

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

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

  10. #40
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #42
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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. #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!

  14. #44
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  15. #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!

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 01:34 AM
  2. function supposed to strip vowels
    By MB1 in forum C++ Programming
    Replies: 11
    Last Post: 04-23-2005, 06:44 PM
  3. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 07:38 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM