| | |
returning number of characters with ASCII code
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 0
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...
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.
Here's what i've done so far...
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> int CountEvenASCII(char []); void main() { char words[100],i,out; clrscr(); for(i=0;i<100;i++);{ printf("Enter a word: "); scanf("%c",&words[i]);} atoi(&words[i]);} printf("ASCII equivalent: %d",words[i]); out=CountEvenASCII(words); printf("\nNumber of characters with even ASCII codes: %d", out); getch(); } int CountEvenASCII(char str[]) { int even_count=0,i; if(str[0]!=NULL){ if(str[i]%2==0){ even_count++; CountEvenASCII(&str[1]); } return even_count; }
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.
C Syntax (Toggle Plain Text)
scanf("%c",&words[i]);} atoi(&words[i]);}
C Syntax (Toggle Plain Text)
// get string from keyboard fgets(words,sizeof(words),stdin); // for(int i = 0; words[i]; i++) { if( (words[i] % 2) == 0) { // even ascii code } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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.
•
•
•
•
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()
•
•
•
•
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].
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Is there any more Documenation that I need? (C++)
- Special Characters in C++ (C++)
- Reading Binary files via stdin (C++)
- need help with an averaging function (C)
- C programming question (C)
Other Threads in the C Forum
- Previous Thread: how the memory is allocated?
- Next Thread: Counting specific characters in a string
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






