scanf("%c",&words[i]);}
atoi(&words[i]);}
above is useless. just input the text as normal characters. You don't have to do any conversion at all to get the ascii value of the characters -- data type char already contains the ascii value. To see if its odd or even
// get string from keyboard
fgets(words,sizeof(words),stdin);
//
for(int i = 0; words[i]; i++)
{
if( (words[i] % 2) == 0)
{
// even ascii code
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I see Ancient Dragon has typed faster than I and with a more precise version of what to do. As a learning experience here's a sampling of ideas I have about your code:
1) use int main(), not void main()
2) i should be an int, not a char
2) if you use a for loop, don't place a semicolon after the ) and before the { in the for loop declaration. It prevents the body of the for loop from being used if you put it there.
3) when using statements requiring braces format your code such that you line up the braces so it's easier to make sure you have matching numbers of opening and closing braces. You appear to have 1 too many closing curly brace in the code you posted.
4) if you're going to use scanf(), and for beginners it's okay, though it's not the best way to obtain input, then read in an entire string with use of %s, words instead of %c, &words[i]. This will prevent the need the for loop you have in the first place. See Ancient Dragon's solution for a better way to get input.
5) if you are going to use atoi(), you really should use the return value of atoi() in some way; but you shouldn't use it here at all
6) don't use recursion until you are familiar with it unless you absolutely have to. In this case, within CountEvenASCII() you'd be better off using a loop to look at every char in the string sent to the function. See Ancient Dragon's solution.
7) As a beginning programmer you should make your intentions absolutely clear, so I'd cast the char to type int before calling the modulo operator on it. When you get more experience, like Ancient Dragon, and think int every time you see math operators used on char types, then you probably don't need to write the cast.
All of the above are issues commonly seen in code written by beginners, and more frequently than I like to think, in code I write still.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
I see Ancient Dragon has typed faster than I and with a more precise version of what to do. As a learning experience here's a sampling of ideas I have about your code:
1) use int main(), not void main()
Yes, see this
3) when using statements requiring braces format your code such that you line up the braces so it's easier to make sure you have matching numbers of opening and closing braces. You appear to have 1 too many closing curly brace in the code you posted.
Yes, see this
4) if you're going to use scanf(), and for beginners it's okay, though it's not the best way to obtain input, then read in an entire string with use of %s, words instead of %c, &words[i].
No, see this and this . Recommending scanf("%s"...) is the same as recommending gets() All of the above are issues commonly seen in code written by beginners, and more frequently than I like to think, in code I write still. Hmmmm.... :D
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
The code you have given where should that be put?
Do you really want me to tell you where to put it?? :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
you are not making any sense. Where to put what??
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I would not use that code because it has other issues as well. Write your own program to avoid having to debug other people's code.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343