Hey everyone,

I am supposed to write a program that counts the number of vowels in string of characters.
It must include "Struct counter_t (it must be declared as in the code i've written)" and function "int CountVowels (IN text[], OUT Count)". the OUT parameter must be of type counter_t. I've written the code but it doesn't seem to work. Can someone help me please???

-------------------------------------------------------------------------------------------------------------

#include <stdio.h>

typedef struct {
	int a;
	int e;
	int i;
	int o;
	int u;
}counter_t;

int CountVowels (char text[], counter_t Count);

int main ()
{
	char text[100];
	counter_t Count;
	Count.a = 0;
	Count.e = 0;
	Count.i = 0;
	Count.o = 0;
	Count.u = 0;	
	printf ("Text\n");
	gets(text);
	CountVowels(text, Count);
	printf("a = %d\n", Count.a);
	printf("e = %d\n", Count.e);
	printf("i = %d\n", Count.i);
	printf("o = %d\n", Count.o);
	printf("u = %d\n", Count.u);
	fflush (stdin);
	getchar ();
	return 0;
}

int CountVowels (char text[], counter_t Count)
{
	int i;
	for (i = 0; text[i] != '\0', i++)
	{
		switch(text[i])
			{
				case 'a': Count.a++; break;
				case 'e': Count.e++; break;
				case 'i': Count.i++; break;
				case 'o': Count.o++; break;
				case 'u': Count.u++; break;
			}
	}
	return (Count);
}

------------------------------------------------------------------------------------------------------

Since CountVowels() returns an integer, then the OUT parameter must be a pointer int CountVowels (char text[], counter_t* Count); I don't know what integer that function is supposed to return -- certainly not a counter_t object because that isn't an integer. If you make the second parameter a pointer as * showed above then you must also change the dot notation to pointer notation case 'a': Count->a++; break;

for (i = 0; text[i] != '\0', i++) Change to a (;) gets(text); Very, very bad idea. Forget there's such a function. Not safe. fgets(text, sizeof text, stdin); is what you want. fflush (stdin); fflush() accepts only an output stream; anything else is undefined behavior.

int CountVowels (char text[], counter_t Count)

If CountVowel returns an int, you can not try to force it to return a structure of type counter_t like: return (Count); counter_t CountVowel(char text[], counter_t Count) could be a possibility

Next time use code tags to present your code. Read here

well there's still somethin' wrong. Here's the output of this program:
Text
dfhsdaaahkd
a = 0
e = 0
i = 0
o = 0
u = 0

Assuming you don't use pointers yet:
Change the definition of your CountVowels, and prototype it accordingly. counter_t CountVowels(char text[], counter_t Count) Then make the correct call to it in main. Count = CountVowesl(text, Count);

wow!!! thanx guys!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.