944,222 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2118
  • C RSS
Sep 29th, 2009
-1

count number of characters program in C

Expand Post »
i need help writing a program that counts the characters entered into a program untill EOF is reached. I have to use the getchar function and my program should output the number of characters entered.


thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bbballin is offline Offline
3 posts
since Sep 2009
Sep 29th, 2009
0

Re: count number of characters program in C

We like to help those that show some effort, especially when it appears to be an assignment.

Show us what you've tried. Tell us what it does and what you expected it to do and we can help you make the two match up better.

(I wouldn't be surprised to find an example program kicking around here somewhere...did you do a search before you started a topic?)
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Sep 29th, 2009
0

Re: count number of characters program in C

Click to Expand / Collapse  Quote originally posted by Murtan ...
We like to help those that show some effort, especially when it appears to be an assignment.

Show us what you've tried. Tell us what it does and what you expected it to do and we can help you make the two match up better.

(I wouldn't be surprised to find an example program kicking around here somewhere...did you do a search before you started a topic?)
sorry i forgot to post my code

  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5. int i ;
  6. int c;
  7.  
  8. while ( ( i = getchar() ) != EOF )
  9. c++ ;
  10.  
  11. printf( "%d characters\n" , c) ;
  12.  
  13. return 0;
  14. }

does this seem ok? ... when i run it ./a.out i click enter
then i enter a word say "daniweb" and press enter and then hit ctrl+d and it says 8 characters
Last edited by bbballin; Sep 29th, 2009 at 8:04 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bbballin is offline Offline
3 posts
since Sep 2009
Sep 29th, 2009
0

Re: count number of characters program in C

That looks like it ought to work.

The program is counting the characters in the word say "daniweb" and also counting the enter as it is also a character before the EOF.

if you entered "daniweb" ctrl+d enter, it should count 7.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Sep 30th, 2009
0

Re: count number of characters program in C

yes it will work altough u need to work on your indentation
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Oct 2nd, 2009
1

Re: count number of characters program in C

Click to Expand / Collapse  Quote originally posted by bbballin ...
sorry i forgot to post my code

  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5. int i ;
  6. int c;
  7.  
  8. while ( ( i = getchar() ) != EOF )
  9. c++ ;
  10.  
  11. printf( "%d characters\n" , c) ;
  12.  
  13. return 0;
  14. }

does this seem ok? ... when i run it ./a.out i click enter
then i enter a word say "daniweb" and press enter and then hit ctrl+d and it says 8 characters
where is c initialized ?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008

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: isAnagram() function in C
Next Thread in C Forum Timeline: c program





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


Follow us on Twitter


© 2011 DaniWeb® LLC