# Strings question

• 03-14-2005
kimimaro
Strings question
I am doing a program to count even ASCII characters in a string for instance, when the string is input "ABCDE", there is a recursive function within the program to display 2 where both B and D are even ASCII characters. Now my question is how and what function you use to examine the characters of a string one by one to see if its an even ASCII character?
• 03-14-2005
hk_mp5kpdw
The modulus (%) operator.

Code:

```char cVal; ... if( !(cVal % 2) ) {     // It is an even character } else {     // It is an odd character }```
• 03-14-2005
kimimaro
what is the function cVal for? Sorry but the function should be input by string but can the function accept string?
• 03-14-2005
InvariantLoop
cVal is not a function, its the character you are testing to see if it is even or odd.
• 03-14-2005
hk_mp5kpdw
Quote:

Originally Posted by kimimaro
what is the function cVal for?

cVal is not a function, it is a simple char data type and just used as an example of how to test whether the given character is odd or even. In your actual code you would be testing whatever character you were currently looking at in your "string".

Quote:

Originally Posted by kimimaro
Sorry but the function should be input by string but can the function accept string?

This is the c-board and not the c++ board. By "string" I take it to mean you are dealing with a NULL terminated character array? Why don't you show us some of the code you are currently working on?
• 03-14-2005
InvariantLoop
I think what he is trying to do is, input A or C or any character for that matter, and find if the numeric value of A for example is even.
• 03-14-2005
kimimaro
yes and the question that I'm dealing with is that it stated that I must use Recursive function and must accept string value. From my senese is that the input should measure the length of the string and then the recursive will stop after all the numbers of character of the string is finished. I dont know how to use the strlen function eventhough I've read it in the tuitorial and I dont know what function should I use to one-by-one operates the characters within the single string
• 03-14-2005
kimimaro
That question is as below :

Write a recursive function called CountEvenASCII that takes in a string parameter str and return the number of characters with even ASCII codes. For example CountEvenASCII("ABCDE") returns 2 (as B and D are of even ASCII codes).
• 03-14-2005
InvariantLoop
Well do you have anything done so far? Do you know how to find the numeric value of A or B or C. Do you know how to find if those values are even? Do the easy things first and then figure out how to make everything work.
• 03-14-2005
kimimaro
I've done the recursive and the putchar function so far. To find even numbers, (str%2==0)
But I am no good with strings what I want now is how to get each alphabet in the string to check if its even
• 03-15-2005
quzah
The exact same way. Do you know how to move through an array? If so, then do so, and compare that character to your even/odd test.

Quzah.