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

Recommended Answers

All 5 Replies

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?)

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

#include <stdio.h>

int main() {

int i ;
int c;

while ( ( i = getchar() ) != EOF )
c++ ;

printf( "%d characters\n" , c) ;

return 0;
}

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

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.

yes it will work altough u need to work on your indentation

sorry i forgot to post my code

#include <stdio.h>

int main() {

int i ;
int c;

while ( ( i = getchar() ) != EOF )
c++ ;

printf( "%d characters\n" , c) ;

return 0;
}

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 ?

commented: Tada! The actual thread solver. +24
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.