# Thread: Counting characters based on certain parameters

1. ## Counting characters based on certain parameters

Hello guys,

I was wondering, how do you build a program that will assign certain values to certain characters and different values to others?

For example, if I wanted to assign a value of 1 to vowels and 2 to consonants, how exactly would I go about doing this? And how would I make it so it ignores any other characters (!@#%#\$^ etc.) ?

Thanks

2. You can implement 2 arrays, one for the 1-valued characters, and other for the 2-valued; then simply read char by char and check out on which array is; for the ignored characters, if they all are non vowels nor consonants you can use a simple ascii trick instead of search them on the arrays.

Hope that helps

3. Thank you for the response, Niara. It sheds a little light, but I am still very new to C programming unfortunately and so I am still a little lost.

Is there a resource you can guide me to that will help me out on such matters? Also, the value will be based on user input; they will be able to decide how much a vowel/consonant is worth. And how do I use ASCII values in the search instead?

4. Ehh... Is the user going to be asked a question and then he, or she, is supposed to answer. Like
You favourite superhero.
(1) Kermit the frog
(2) Winnie the Poo
(3) Hulk
Or... are you going to encode what they type in?

5. Basically the program operates like this:

Enter value for Vowels: %d \n
Enter value for Consonants: %d \n

Enter string: %s \n

Total for vowels: %d \n
Total for consonants: %d \n
---------------------------
It calculates a total for each vowel*inputvalue and likewise for consonants

6. Write these functions:
Code:
int isVowel(char ch) {
/* Returns 1 if ch is a vowel
* otherwise returns 0 */
}

int isConsonant(char ch) {
/* Returns 1 if ch is a consonant
* otherwise returns 0 */
}
Then write your main program using those functions.

7. I'd have thought it'd make more sense to just have 1 function

Code:
int getLetterValue(char ch)
{
return 1 if vowel
return 2 if consonant
return 0 otherwise
}
In answer to your original question OP, no, there isn't really a way you can do what you're asking. Probably the quickest (in terms of exec time) would be to have an array of 26 values, indexed by letter and containing the value. So it'd be

Code:
{1,2,2,2,1 ...)
You'd have to handle accesses outside of the array (e.g. digits, special characters), and do a bit of calculation on the input ASCII value to get it into 0-25 range. Nothing very tricky, but not trivial. It may be better to just implement a large switch statement in getLetterValue. Don't forget to handle both upper and lower case, or convert to all one case first.

8. Originally Posted by smokeyangel
I'd have thought it'd make more sense to just have 1 function
I concur. However, I was attempting to assess the OP's skill level. If they can't correctly create the isVowel function, then anything more is a pipe dream.

9. you could simply create the TOUPPER macro that converts all the alphabets to upper case and then use the array that designates the case provided by @smokeyangel together with the ascii trick successfully check the number of consonants and vowels .....