By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,538 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,538 IT Pros & Developers. It's quick & easy.

Program to count number of characters and lines in input

P: 1
Hi there, I've just started to learn c and there's some sections which i have no idea of. I'm hoping some one can help me.

I'm required to write a program which counts the number of characters and lines in the input
and this is what the output should look something like this:
I know that I will need to use getchar() for this but how does it count the characters?


[@localhost ~]$ ./count
Enter text:
Mary had a little lamb,
little lamb, little lamb;
Mary had a little lamb,
its fleece was white as snow.
^D
Lines: 4
Chars: 104
Nov 11 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 196
I believe what you need to do is to find the length of the entire string, e.g. use strlen(examplestring) this will find the length of string, note it counts spaces as well. And in regards to the number of lines you could try searching the string for line breaks or line returns, i believe they have a specific ascii value that you could find by googling.

hope this helps
Nov 12 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
I know that I will need to use getchar() for this but how does it count the characters?
Okay, think about that. What do you think a function called getchar() would do? Now, what can you do each time this function is called to count the number of characters? How would you keep track of this without a computer, if you were just given a song and told to count the number of characters? Would you possibly store it somewhere?
Nov 12 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.