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

  1. #16
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by quzah
    Quote Originally Posted by nadroj
    I dont know what you mean here, but theres no real tricks or magic you can do to solve this, so dont look for things like that. Use the method I suggested above/earlier.
    I actually wouldn't use strchr.
    I never suggested to use any function, let alone "strchr". Your pseudocode is what I was suggesting earlier. I think you suggested to create a function which checks if a char is a vowel or not, and call it each time in the for loop. In that function, you would need to use a for loop. So both methods are essentially two nested for loops.

  2. #17
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    I never suggested to use any function, let alone "strchr". Your pseudocode is what I was suggesting earlier. I think you suggested to create a function which checks if a char is a vowel or not, and call it each time in the for loop. In that function, you would need to use a for loop. So both methods are essentially two nested for loops.
    Yes, and you are right, I'm not thinking in using any function or creating one.

    I'm still trying to manage solving/writing the program with the two nested for loops.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nadroj View Post
    I never suggested to use any function, let alone "strchr".
    Sorry, I confused you with the guy right below you.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    No worries.

    georgio777, my suggestion earlier and the pseudocode suggested by quzah do the same thing, one is directly using a nested for loop, the other is using a for loop and calling a function inside (which has a for loop)--either way they are nested for loops. So you can refer to his pseudocode earlier and just replace the function part with another for loop
    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

  5. #20
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    No worries.

    georgio777, my suggestion earlier and the pseudocode suggested by quzah do the same thing, one is directly using a nested for loop, the other is using a for loop and calling a function inside (which has a for loop)--either way they are nested for loops. So you can refer to his pseudocode earlier and just replace the function part with another for loop
    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
    As my understand the code should look like this:

    Code:
    int i, j;
    
    for(i=0; str[i] != '\0'; i++)
    {
    	for(j=0; strv[j] != '\0'; j++)
    	{
    		if (str[i] == strv[j])
    		{
    		printf("*");
    		}
    		else
    		{
    		printf(" ");
    		}
    	}
    }
    And it works only that there are HUGE spaces between each *

    i.e.

    OUTPUT

    Code:
    abeci
    *                    *                    *

  6. #21
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Its because your code doesnt match my pseudocode. I dont like to just give answers/solutions (which is why I waited to post any pseudocode), and I feel that the pseudocode already gave it away. If you look closely to every single thing in the pseudocode I posted, you should be able to spot whats different (another spoiler: do you know what the "break" statement does?)

  7. #22
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    Its because your code doesnt match my pseudocode. I dont like to just give answers/solutions (which is why I waited to post any pseudocode), and I feel that the pseudocode already gave it away. If you look closely to every single thing in the pseudocode I posted, you should be able to spot whats different (another spoiler: do you know what the "break" statement does?)

    Ok, you're right, I agree with you, it's better not to give the answers, so let's try again.
    And yes I know what break does.

    Code:
    int i, j;
    
    for(i=0; str[i] != '\0'; i++)
    {
    	for(j=0; strv[j] != '\0'; j++)
    	{
    		if (str[i] == strv[j])
    		{
    		printf("*");
    		}
    	break;
    	}
    	printf(" ");
    }
    In this case it only works on the first vowel.
    Last edited by georgio777; 11-16-2009 at 08:35 PM.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are printing a space every time, even if you find a vowel.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    You are printing a space every time, even if you find a vowel.


    Quzah.
    Not with the break statement.
    Even though I need to fix that.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by georgio777 View Post
    Not with the break statement.
    Maybe if you actually put it in the right spot... but not the way you're doing it now. You actually need to see if you assign a vowel, and if so, don't put a space.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    Maybe if you actually put it in the right spot... but not the way you're doing it now. You actually need to see if you assign a vowel, and if so, don't put a space.


    Quzah.
    That's why I'm trying to do, but I'm unsure about the break statement, because as I can see when the compiler passes through the breaking point, it stops the complete for loop.

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for ...
        for ...
            if this then break
        this is the outer for, your break doesn't break from this loop
    Therefore, you need to add another check in there, and if you've assigned a vowel, you need to not put a space.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    Code:
    for ...
        for ...
            if this then break
        this is the outer for, your break doesn't break from this loop
    Therefore, you need to add another check in there, and if you've assigned a vowel, you need to not put a space.


    Quzah.
    When you said another check, you mean a if statement?

    And the space is when is NOT a vowel.

    For instance as I gave a example before:

    Code:
    first
     *
    The * only appears on the vowels.


    It's crazy I can't make it work !!!

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to write it out, and then think about what you wrote, what each step means:
    Code:
    for each letter in the source string ... ok, how do I do that?
        if this letter is a vowel ... ok how do I do that?
            do a star or whatever ... ok how do I do that?
        else
            do the opposite of whatever (star/space) ... ok how do I do that?
    You can do the first, you just use a for loop. Now, how do you do the second?


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    You need to write it out, and then think about what you wrote, what each step means:
    Code:
    for each letter in the source string ... ok, how do I do that?
        if this letter is a vowel ... ok how do I do that?
            do a star or whatever ... ok how do I do that?
        else
            do the opposite of whatever (star/space) ... ok how do I do that?
    You can do the first, you just use a for loop. Now, how do you do the second?


    Quzah.
    Ok, let see:

    for each letter in the source string ... ok, how do I do that?
    #1 The source string is char str[100];
    #2 Since is for each letter I need to create a for loop
    #3 The int i; is the letter and the i++ is for each letter
    #4 The str[i] != '\0' is for stopping the loop when the word is at the end of the string

    The result:
    Code:
    for(i=0; str[i] != '\0'; i++)
    if this letter is a vowel ... ok how do I do that?

    #1 To find if the letter is a vowel I need to compare each character with the other vowel string
    #2 To do that I need to create a nested for loop which is:
    Code:
    for(j=0; strv[j] != '\0'; j++)
    #3 Where the int j; is the vowel, j++ is each vowel and the str[j] != '\0' is for stopping the loop when the word is at the end of the string
    #4 Now each time the loop is working it should make the comparison with the vowels. For doing that you need to create an if statement in this case:
    Code:
    if (str[i] == strv[j])
    {
    	printf("*");
    }
    else do the opposite of whatever (star/space) ... ok how do I do that?

    #1 For that I create an else statement:

    Code:
    else
    {
    	printf(" ");
    }

    So what's wrong?

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, 12:34 AM
  2. function supposed to strip vowels
    By MB1 in forum C++ Programming
    Replies: 11
    Last Post: 04-23-2005, 05:44 PM
  3. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06: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