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

  1. #1
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    Function with array to find number of vowels and consonants

    Hi all, I am trying to make a program capable to find with "*" the quantity of vowels and consonants that a input word may have.

    i.e.

    If I input the word first the output for finding vowels should be:

    Code:
    first
     *
    And for consonants should be:

    Code:
    first
    * ***
    Hope you get an idea!
    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have lots of ideas. But then, I don't actually care about finishing this program and getting a grade for it. So, what are your ideas?


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

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    As mentioned, tell us what you've done. In general, programming implementation details are usually straightforward. That is, once you know the problem and solution, its not very difficult to translate the solution to some programming language.

    So, basically try and think how a person would solve this problem, for any given input. Because you have to consider any given input, you must give explicit details that will always work. This is basically what "pseudocode" is--an English description of the solution. So, whats your English description of the solution, or at least an attempt at it?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Find the standard library function that finds a character in a string. Establish a string of the vowels: "aeiou" (and sometimes 'y'?). Step through each letter in the input word and see if it's present in the vowel inventory. Print a '*' if yes.... or a space... or do the opposite print effect for showing consonants.

  5. #5
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    I have lots of ideas. But then, I don't actually care about finishing this program and getting a grade for it. So, what are your ideas?


    Quzah.
    Sorry I forgot to put what I have done by now:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "arrayv.h"
    
    /* This function will find the number of vowels in the word and output their position using a * to
    mark the vowels position. */
    int arrayv()
    {
    	int i;
    	char strv[11] = {'a','e','i','o','u','A','E','I','O','U','\0'};
    	char str[100], repeat;
    	printf("Number of vowels in a word:\n");
    	do{
    	printf("\n");
    	printf("Please enter a word: ");
    	scanf("%s", str);
    	printf("\n");
    	printf("%s\n", str);
    	for(i=0; str[i] != '\0'; i++)
    	{
    		if (str[i] == strv[i])
    		{
    			printf("*");
    		}
    		else
    		{
    			printf(" ");
    		}
    	}
    	
    	
    	printf("\n");
    	printf("\n");
    	printf("Play Again? ");
    	getchar();
    	repeat = getchar();
    	while ( getchar() != '\n');
    	} while(repeat == 'Y' || repeat == 'y');
    	
    	return EXIT_SUCCESS;
    }
    With this code, it works sometimes, but not always and is only vowels.

    NOTE:It looks like the problem is where I marked, probably I'm not really writing the correct way the if statement.
    Last edited by georgio777; 11-16-2009 at 06:54 PM.

  6. #6
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    As mentioned, tell us what you've done. In general, programming implementation details are usually straightforward. That is, once you know the problem and solution, its not very difficult to translate the solution to some programming language.

    So, basically try and think how a person would solve this problem, for any given input. Because you have to consider any given input, you must give explicit details that will always work. This is basically what "pseudocode" is--an English description of the solution. So, whats your English description of the solution, or at least an attempt at it?
    Yeah, sorry for that. I forgot one of the main rules of this Forum.

  7. #7
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nonoob View Post
    Find the standard library function that finds a character in a string. Establish a string of the vowels: "aeiou" (and sometimes 'y'?). Step through each letter in the input word and see if it's present in the vowel inventory. Print a '*' if yes.... or a space... or do the opposite print effect for showing consonants.
    Thanks, it's sort like what I did, in exception that I can't find the standard library function to find the character in the string. As my guess is:

    strchr()
    Last edited by georgio777; 11-16-2009 at 06:56 PM.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by georgio777 View Post
    Code:
        for(i=0; str[i] != '\0'; i++)
        {
            if (str[i] == strv[i])
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
    Your checking index i of the string with index i of the "vowels". For example, the string "iae" you would check "i == a", "a == e", and "e == i", so it would report no vowels when in fact they are all vowels. Also, say the string entered was 15 chars, you will be comparing something like (among the other previous comparisons) "str[15] == strv[15]", which is obviously going past the bounds of the array "strv".
    Basically, you need to check every character of your string, with every character of the "vowels". So you need two for loops, your outer one plus an inner one.

  9. #9
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    Your checking index i of the string with index i of the "vowels". For example, the string "iae" you would check "i == a", "a == e", and "e == i", so it would report no vowels when in fact they are all vowels. Also, say the string entered was 15 chars, you will be comparing something like (among the other previous comparisons) "str[15] == strv[15]", which is obviously going past the bounds of the array "strv".
    Basically, you need to check every character of your string, with every character of the "vowels". So you need two for loops, your outer one plus an inner one.
    Oh, you are right, I'm comparing the same index of the input word with the one of the string index of the vowels.

    My question is, in the for loops, both of them must include the if statement or the if statement is useless?

    I was thinking as something similar to finding the biggest or smallest number of an array, in this case all the characters must be in the memory so they can be easily compared. Am I right?
    Last edited by georgio777; 11-16-2009 at 07:03 PM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    My question is, in the for loops, both of them must include the if statement or the if statement is useless?statement
    You definitely need to compare "something", so the if statement is required and not useless. If you dont know if you need just one if statement inside the inner-most for loop, or if you need two (one inside the outer and inner for loop), then ask yourself what would either of these if statements be doing:
    - if you put one in the outer for loop, what would it be checking?
    - if you put one in the inner for loop, what would it be checking?
    - if you put two (one in outer, one in inner) for loop, what would it be checking?

    Hopefully you see that two of these three options dont really make sense, in which case the remaining choice is correct.

    I was thinking as something similar to finding the biggest or smallest number of an array, in this case all the characters must be in the memory so they can be easily compared. Am I right?
    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.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nadroj View Post
    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 mean you could, but I don't really see the point. Instead you'd be better served writing something like isvowel, and running through the string in a loop, and tagging either vowels or non-vowels. I'd probably just do something like:
    Code:
    for each character in the source string
        if this string's current character is  a vowel (or non vowel, depending what you want)
            set destination string (here) to be a star
        else
            set destination string (here) to be not-star
    print source string and newline
    print destination string and newline
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    You definitely need to compare "something", so the if statement is required and not useless. If you dont know if you need just one if statement inside the inner-most for loop, or if you need two (one inside the outer and inner for loop), then ask yourself what would either of these if statements be doing:
    - if you put one in the outer for loop, what would it be checking?
    - if you put one in the inner for loop, what would it be checking?
    - if you put two (one in outer, one in inner) for loop, what would it be checking?

    Hopefully you see that two of these three options dont really make sense, in which case the remaining choice is correct.

    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 fell really dumb since I don't really understand what are you trying to say.

    I know that I need to do is nested for loops but I don't understand why. I sort got an idea that each character of the input word must pass thought the for loop so it can check if the character is or not a vowel, then when finished, continue with the second character and repeat the process until the word is finished.

    But I don't know how to do that!

  13. #13
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    I actually wouldn't use strchr. I mean you could, but I don't really see the point. Instead you'd be better served writing something like isvowel, and running through the string in a loop, and tagging either vowels or non-vowels. I'd probably just do something like:
    Code:
    for each character in the source string
        if this string's current character is  a vowel (or non vowel, depending what you want)
            set destination string (here) to be a star
        else
            set destination string (here) to be not-star
    print source string and newline
    print destination string and newline
    Quzah.
    Oh, you're right, the teacher said something about isvowel, let see if I can do a little bit of more research related to that function!

    Thanks!

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by georgio777 View Post
    Oh, you're right, the teacher said something about isvowel, let see if I can do a little bit of more research related to that function!

    Thanks!
    They probably said you need to write one. There is no standard C 'isvowel' function.


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

  15. #15
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    They probably said you need to write one. There is no standard C 'isvowel' function.


    Quzah.
    Oh, ok. Probably I'm gonna stick with nadroj idea.

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