473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

1.5.2 Character Counting

Alright, so I'm a little confused here...what exactly does this do?
I've run it and it doesn't display anything, so I've typed some things
into it, to see if it'd do something then, but to no avail. Can
somebody explain to me what this does?:

#include <stdio.h>

/* count characters in input; 2nd version */
main()
{
double nc;

for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

It's confusing the way they say it. Any help is GREATLY appreciated!

Nov 14 '05 #1
5 1985

Matt wrote:
Alright, so I'm a little confused here...what exactly does this do?
I've run it and it doesn't display anything, so I've typed some things into it, to see if it'd do something then, but to no avail. Can
somebody explain to me what this does?:

#include <stdio.h>

/* count characters in input; 2nd version */
main()
int main(void)
{
double nc;

for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
return 0;
}

It's confusing the way they say it. Any help is GREATLY appreciated!


It reads the number of characters in a file and prints that number out.
The file, in this case, is stdin.

(Iif you have access to a Unix system)
Try executing the program like this:

$ cat FILENAME | ./PROGRAM

where FILENAME is some file, and PROGRAM is your a.out (or whatever you
are calling this).

-Jason

Nov 14 '05 #2
How would I do that on PC?(Windows XP SP2)

Nov 14 '05 #3

Matt wrote:
How would I do that on PC?(Windows XP SP2)


I'm not knowedgeable about XP console stuff. Try simply running the
program and typing in some data. Then press CONTROL-Z to send EOF. It
should show how many chars you typed.

Nov 14 '05 #4
"Matt" <no******@gmail.com> writes:
How would I do that on PC?(Windows XP SP2)


How would you do what?

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

<OT>
Jason recommended:

$ cat FILENAME | ./PROGRAM

A simpler way to do this is

$ ./PROGRAM < FILENAME

The same thing should work under a Windows command prompt (but you'll
probably need to change ./PROGRAM to .\PROGRAM).

This is way off-topic for comp.lang.c; if you have any more
Windows-specific questions, you'll have better luck in a
Windows-specific newsgroup (if the on-line help doesn't answer
your questions).
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Groovy hepcat Matt was jivin' on 19 Apr 2005 14:57:52 -0700 in
comp.lang.c.
1.5.2 Character Counting's a cool scene! Dig it!
Alright, so I'm a little confused here...what exactly does this do?
I've run it and it doesn't display anything, so I've typed some things
into it, to see if it'd do something then, but to no avail. Can
somebody explain to me what this does?:

#include <stdio.h>

/* count characters in input; 2nd version */
main()
{
double nc;
Liar, liar! Pants on fire! K&R2 has no such thing in section 1.5.2.
There the real nc is a long, not a double.
for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

It's confusing the way they say it. Any help is GREATLY appreciated!


As I said before in another thread, and still not trying to be mean
or cause offence, if you can't understand such a simple program when
its purpose is clearly explained on the same (and a little on the
previous) page, then programming is probably not the best thing for
you.
This program counts the number of characters in its input. Clear?
You type in any characters at all, or redirect/pipe input from another
source such as a file (if your system allows it), and at the end it
outputs the number of characters typed/piped/read. How do you let the
program know that you want to end the input when you are entering it
manually from a keyboard? Well, that's a FAQ. I'm going to tell you
just this once, but in future please consult the FAQ list (see URL
below). It depends. It depends on the system. For example, on
DOS/Windows you type Ctrl + z followed by enter.
The FAQ can be found at http://www.eskimo.com/~scs/C-faq/top.html.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
18
by: Toto | last post by:
Hello, my problem is quite simple to explain. I have the following string: "table+camera" and I want to remove the + sign: "tablecamera". How do i do that ?
44
by: Michael McGarry | last post by:
Hi, Is there a standard library function that counts the number of occurences of a character in a string? Regards, Michael
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
22
by: gene.ariani | last post by:
I have a string like the following: 10AF101-25 I would like to extract any numerical number that precedes the "-" and stops when it encounters any string character like AF So my result...
39
by: Umesh | last post by:
Plese help. Is there any software by which we can do that?
4
by: Anthony P. | last post by:
Hello Everyone, I'm building an application and need to keep track of the amount of text in a textbox to make sure the user knows how many characters they have left to type. I've set the...
16
by: bgold12 | last post by:
Will newlines ever be standardized? I recently discovered that in a textarea, internet explorer adds \r\n for every newline you enter, while firefox adds \n. I know \r is also used in some...
6
by: gita ziabari | last post by:
Hello All, The following code does not work for unicode characters: keyword = dict() kw = 'генских' keyword.setdefault(key, ).append (kw) It works fine for inserting ASCII character. Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.