Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,279 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,231 people online right now. Registration is fast and FREE... Join Now!




Mis-counts the number of vowels in a string

 
Reply to this topicStart new topic

Mis-counts the number of vowels in a string

camaroer87
post 14 Mar, 2008 - 04:17 PM
Post #1


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 40

Here is my code:
CODE

int count_vowels(int n, char a[])
{
    char vowels[] = "aeiouAEIOU";
    int i, j;
        
    n = 0;
    
    for(i = 0; vowels[i] != NULL; i++)
        for (j = 0; vowels[j] != NULL; j++)
            if(vowels[j] == a[i])
            {
                n++;
                break;
            }
    
    return n;
}


When I enter Derek Wolters as the string it says there are 3 vowels (1 less than it should), when I enter Kim is cool as the string it says there are 4 vowels (the correct number). What is wrong here?
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 04:26 PM
Post #2


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


The first for loop should be traversing through the char-array a[] and not vowels[]. I haven't tested it yet.
User is offlineProfile CardPM

Go to the top of the page

camaroer87
post 14 Mar, 2008 - 04:31 PM
Post #3


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 40

That says only 2 vowels for Derek Wolters
User is offlineProfile CardPM

Go to the top of the page

camaroer87
post 14 Mar, 2008 - 04:38 PM
Post #4


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 40

heres the full code for the program:
CODE

#include <string.h>
#include <stdio.h>
#define MAX 225

int count_char(int n, char a[]);
int count_words(int n, char a[]);
int count_vowels(int n, char a[]);

int main(void)
{
    char line [MAX];
    int a, b, c;
    
    printf("Input a line of text: ");
    gets(line);
    
    a = count_char(a, line);
    b = count_words(b, line);
    c = count_vowels(c, line);
    
    printf("\nThere are %d characters, %d words, and %d vowels in the line.", a, b, c);
    
    return 0;
}

int count_char(int n, char a[])
{
    n= strlen(a);        
    return n;
}

int count_words(int n, char a[])
{    
    char* token;
    
    n=0;
    token = strtok(a, " ");
    
    while(token != NULL)
    {
        n++;
        token = strtok(NULL, " ");
    }
    
    return n;
}

int count_vowels(int n, char a[])
{
    char vowels[] = "aeiouAEIOU";
    int i, j;
        
    n = 0;
    
    for(i = 0; vowels[i] != NULL; i++)
        for (j = 0; vowels[j] != NULL; j++)
            if(vowels[j] == a[i])
            {
                n++;
                break;
            }
    
    return n;
}
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 04:41 PM
Post #5


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


I haven't seen your new code, but here is mine:

CODE

int count_vowels(int n, char a[])
{
    char vowels[] = "aeiouAEIOU";
    int i, j;        
    n = 0;
    
    for(i = 0; a[i] != '\0'; i++){
        for (j = 0; vowels[j] != '\0'; j++){
            if(a[i] == vowels[j])
            {
                n++;
                break;
            }
        }
    }
    
    return n;
}
User is offlineProfile CardPM

Go to the top of the page

camaroer87
post 14 Mar, 2008 - 04:47 PM
Post #6


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 40

That says that there are 5 vowels for Derek Wolters. and no vowels for Kim is cool. Hmm...One less, one more... third trys a charm...

This post has been edited by camaroer87: 14 Mar, 2008 - 04:48 PM
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 04:47 PM
Post #7


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


I tested your entire code, and the count_words() function is messing up the things.
User is offlineProfile CardPM

Go to the top of the page

camaroer87
post 14 Mar, 2008 - 04:49 PM
Post #8


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 40

should I use a different variable instead of n?

edit* that didnt work

should I create a copy of the string in the count_words and use that

This post has been edited by camaroer87: 14 Mar, 2008 - 04:53 PM
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 04:56 PM
Post #9


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


I have changed the count_words() :

CODE

int count_words(int n, char a[])
{    
    
    int i=0;
    n=0;
    
    
    
    while(a[i] != '\0') {
        if(a[i]==' ')
            n++;
        i++;        
    }

    return n+1;

    //token = strtok(a, " ");//char* token;//token = strtok(NULL, " ");
}
User is offlineProfile CardPM

Go to the top of the page

camaroer87
post 14 Mar, 2008 - 05:00 PM
Post #10


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 40

GREAT IT WORKS! THANKS! what was the deal? shouldnt the functions be exclusive?
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 05:02 PM
Post #11


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


There was something wrong with this:

//token = strtok(a, " ");//char* token;//token = strtok(NULL, " ");
}


*edit: I assume, the above statements were messing up your input string.

This post has been edited by letthecolorsrumble: 14 Mar, 2008 - 05:13 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 12:43AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month