944,144 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4768
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 5th, 2007
0

returning number of characters with ASCII code

Expand Post »
hi everyone! i've been trying to make a program regarding returning the number of characters with even ASCII codes. For example, if a user input ABCDE, the output should be 2 since B and D are of even ASCII codes.
Here's what i've done so far...

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. int CountEvenASCII(char []);
  6.  
  7. void main()
  8. {
  9. char words[100],i,out;
  10. clrscr();
  11. for(i=0;i<100;i++);{
  12. printf("Enter a word: ");
  13. scanf("%c",&words[i]);}
  14. atoi(&words[i]);}
  15. printf("ASCII equivalent: %d",words[i]);
  16. out=CountEvenASCII(words);
  17. printf("\nNumber of characters with even ASCII codes: %d", out);
  18. getch();
  19. }
  20.  
  21. int CountEvenASCII(char str[])
  22. {
  23. int even_count=0,i;
  24.  
  25. if(str[0]!=NULL){
  26. if(str[i]%2==0){
  27. even_count++;
  28. CountEvenASCII(&str[1]);
  29. }
  30.  
  31.  
  32. return even_count;
  33. }

my problem is it can only return the ASCII equivalent of the first character. For example, if i try to input ABCDE.. the output is 65 instead of 65 66 67..... And also, even if A's ASCII equivalent is an odd number, the program still counts it as an even.. Any idea what I can do about this? Any help with be very much appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaacze is offline Offline
3 posts
since Apr 2007
Apr 5th, 2007
0

Re: returning number of characters with ASCII code

  1. scanf("%c",&words[i]);}
  2. 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
  1. // get string from keyboard
  2. fgets(words,sizeof(words),stdin);
  3. //
  4. for(int i = 0; words[i]; i++)
  5. {
  6. if( (words[i] % 2) == 0)
  7. {
  8. // even ascii code
  9. }
  10. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 5th, 2007
1

Re: returning number of characters with ASCII code

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.
Last edited by Lerner; Apr 5th, 2007 at 2:09 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 5th, 2007
0

Re: returning number of characters with ASCII code

Click to Expand / Collapse  Quote originally posted by Lerner ...
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
Click to Expand / Collapse  Quote originally posted by Lerner ...
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
Click to Expand / Collapse  Quote originally posted by Lerner ...
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()

Click to Expand / Collapse  Quote originally posted by Lerner ...
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....
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Apr 5th, 2007
0

Re: returning number of characters with ASCII code

hi! thanks a lot for the help regarding the program as well as ideas... helped a lot.. have the program working now.....
i've seen what i was doing wrong a while ago.. took me time to understand, but i learned.. thanks agen!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zaacze is offline Offline
3 posts
since Apr 2007
Oct 3rd, 2010
0
Re: returning number of characters with ASCII code
  1. scanf("%c",&words[i]);}
  2. 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
  1. // get string from keyboard
  2. fgets(words,sizeof(words),stdin);
  3. //
  4. for(int i = 0; words[i]; i++)
  5. {
  6. if( (words[i] % 2) == 0)
  7. {
  8. // even ascii code
  9. }
  10. }
The code you have given where should that be put?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frexseroeyt is offline Offline
3 posts
since Oct 2010
Oct 3rd, 2010
0
Re: returning number of characters with ASCII code
Quote ...
The code you have given where should that be put?
Do you really want me to tell you where to put it??
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 3rd, 2010
0
Re: returning number of characters with ASCII code
Do you really want me to tell you where to put it??
Yes, just PM the codes cause i really don't understand where to put the codes..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frexseroeyt is offline Offline
3 posts
since Oct 2010
Oct 3rd, 2010
0
Re: returning number of characters with ASCII code
you are not making any sense. Where to put what??
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 4th, 2010
0
Re: returning number of characters with ASCII code
  1. scanf("%c",&words[i]);}
  2. 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
  1. // get string from keyboard
  2. fgets(words,sizeof(words),stdin);
  3. //
  4. for(int i = 0; words[i]; i++)
  5. {
  6. if( (words[i] % 2) == 0)
  7. {
  8. // even ascii code
  9. }
  10. }
I mean this code above.where should this code be put in "zaacze" given code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frexseroeyt is offline Offline
3 posts
since Oct 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: add data in txt file
Next Thread in C Forum Timeline: Insert data to Mysql using token in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC