Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
c program to count vowels in a string
Posted
Updated 3-Dec-11 11:04am
v2
Comments
[no name] 3-Dec-11 19:09pm    
Jack,

You need to describe what you have accomplished so far and what problems you are having. In the C language you can compare characters as if they are numbers such as: char vowels[] = {'a', 'e', 'i', 'o', 'u'}; if('a' == vowels[0]) {/*...*/}
LanFanNinja 3-Dec-11 19:40pm    
Considering this is homework I hate to just give you the code to do this but I will tell you it can be easily accomplished using a for loop, if statement, and a integer variable (to keep track of the number of vowels). Try on your own and then if you get stuck come back here and post your code and tell us what problems you are having then we can help you more.
Sergey Alexandrovich Kryukov 3-Dec-11 22:37pm    
Not a question. What's the problem?
--SA
Sergey Alexandrovich Kryukov 3-Dec-11 22:38pm    
Only in English or something else?
--SA

Try this:

C#
char[] vowels = {'a','e','i','o','u'};
string input = txtInput.Text.ToLower();
char[] splittedResult = input.Split('');
int ctr = 0;

foreach(char result in splittedResult)
{
   switch(result)
   case vowels[0]:
   case vowels[1]:
   case vowels[2]:
   case vowels[3]:
   case vowels[4]: ctr++;
                   break;
   default: break;
}


The result of ctr will be the count of vowels.

Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
Since this is homework you don't get code!

1) Define and clear a counter.
2) Start a loop through each character in the string
3) If the character is a vowel, increment the counter by one.
3a) This could be done with if...else if...else
3b) Or this could be done with a switch
4) After the loop completes the counter contains the vowel count.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900