count number of characters program in C

Reply

Join Date: Sep 2009
Posts: 3
Reputation: bbballin is an unknown quantity at this point 
Solved Threads: 0
bbballin bbballin is offline Offline
Newbie Poster

count number of characters program in C

 
-1
  #1
Sep 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: count number of characters program in C

 
0
  #2
Sep 29th, 2009
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?)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: bbballin is an unknown quantity at this point 
Solved Threads: 0
bbballin bbballin is offline Offline
Newbie Poster

Re: count number of characters program in C

 
0
  #3
Sep 29th, 2009
Originally Posted by Murtan View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: count number of characters program in C

 
0
  #4
Sep 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: count number of characters program in C

 
0
  #5
Sep 30th, 2009
yes it will work altough u need to work on your indentation
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,151
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: count number of characters program in C

 
1
  #6
Oct 2nd, 2009
Originally Posted by bbballin View Post
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 ?
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC