Counting blanks

R

rajash

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
{
char c;

fflush(stdin); // start reading from stdin
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
}
 
T

Tor Rustad

Hi I am new to this forum.

A number of the basic errors in your code, is explained in the C FAQ.
I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
???

{
char c;
???

fflush(stdin); // start reading from stdin
???

while(! feof(stdin) ) {

No read errors possible?
switch(c = getchar()) {
???

case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up
???


printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
}

Try again, your code doesn't even compile:

$ gcc -ansi -pedantic -W -Wall test.c
test.c:4: warning: return type of âmainâ is not âintâ
test.c: In function âmainâ:
test.c:7: warning: implicit declaration of function âfflushâ
test.c:7: error: âstdinâ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)
test.c:7: error: expected expression before â/â token
test.c:15: error: expected expression before â/â token
test.c:5: warning: unused variable âcâ
$ cat -n test.c
1 int x, y, z;
2
3 void main()
4 {
5 char c;
6
7 fflush(stdin); // start reading from stdin
8 while(! feof(stdin) ) {
9 switch(c = getchar()) {
10 case ' ' : x++; break;
11 case '\t': y++; break;
12 case '\n': z++;
13 }
14 }
15 fclose(stdin); // clean up
16
17 printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
18 }
 
P

pete

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

Globals not needed.
void main()

Nonportable form of main
{
char c;

Wrong type for (c). Should be type int.
fflush(stdin); // start reading from stdin

fflush(stdin) is undefined.
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

If you didn't fopen it, you shouldn't fclose it.

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
}



/* BEGIN blank_counter.c */

#include <stdio.h>

int main(void)
{
int c;
unsigned x, y, z;

x = y = z = 0;
while ((c = getchar()) != EOF) {
switch(c) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
printf("[spaces, tabs, newlines] = [%u, %u, %u]\n", x, y, z);
return 0;
}

/* END blank_counter.c */
 
F

Flash Gordon

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

Why are these declared at file scope? Unless you have a *very* good
reason then this is the wrong thing to do.
void main()

Start at the begining of K&R again. You will see that the return type of
main it int.
{
char c;

fflush(stdin); // start reading from stdin

Where in K&R does is show flushing stdin? Answer, nowhere, because it is
has no meaning as far as C is concerned (although a specific
implementation *could* defined it, but that would only apply to that
implementation). It is particularly pointless at the start of a program
anyway.

Also please don't use // style comments when posting to Usenet. Then can
cause problems with line wrapping.
while(! feof(stdin) ) {

Incorrect use of feof. Please see the comp.lang.c FAQ at
http://c-faq.com specifically question 12.2
switch(c = getchar()) {

You should assign the result of getchar to a variable of type int, not
char, see question 12.1 of the comp.lang.c FAQ.
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;

As a matter of style in my opinion you should have a break on the last
case as well.
}
}
fclose(stdin); // clean up

You don't need to close stdin.
printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);

Now that you know that main returns an int you should return an int.
Either 0 (which means success), EXIT_SUCCESS or EXIT_FAILURE.

I've not checked to see if your program would work with these things fixed.
 
U

user923005

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
{
char c;

fflush(stdin); // start reading from stdin
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);

}

#include <stdio.h>
#include <ctype.h>

int main(void)
{
int c;
unsigned n = 0;
do {
c = getc(stdin);
if (isspace(c)) n++;
} while (c != EOF);
printf("File contains %u space characters.\n", n);
return 0;
}
 
I

Ian Collins

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;
One more thing, learn to give variables meaningful names!
 
C

CBFalconer

I have taken a class in C some time ago but now I am reading
Kernigan and Richie's book to refresh my knowledge. I think I have
forgotten alot and there are no solutions to the exercises. Maybe
some people here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

Missing #include of needed standard headers.
int x, y, z;

void main()

Illegal. main returns an int. Say so.
{
char c;

This should be an int.
fflush(stdin); // start reading from stdin

Illegal. fflush doesn't operate on input files. // comments are
not allowed in C90, and you don't have a C99 compiler. And you
certainly don't want to ignore the first inputs.
while(! feof(stdin) ) {

feof only reflects what happened previously, when getchar return
EOF to signal either end-of-file or error. Don't do this. Fix the
whole while loop to revolve around receiving EOF in an int.
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

No // comments.
printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);

Boom. variadic function call with no prototype.

Boom. Failure to return an int. 0, EXIT_OK, EXIT_FAILURE are
allowed. The macros are in stdlib.h

You have work to do. Get these things right early.
 
J

Jack Klein

On Fri, 30 Nov 2007 13:54:11 -0800 (PST),
Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()

There is no example in either version of K&R that starts with "void
main()".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
A

abhiman.max

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
{
char c;

fflush(stdin); // start reading from stdin
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);



}- Hide quoted text -

- Show quoted text -

hey thanks ur sorce code.i also think that is correct.then u know
about linklist,i want ur help.bcoz i want to know about how to sort
the link list using c language.can u help me?
 
S

santosh

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
{
char c;

fflush(stdin); // start reading from stdin
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);



}
hey thanks ur sorce code.i also think that is correct.

It most certainly is not correct. Did you not read all the multiple
posts correcting his numerous errors?
then u know
about linklist,i want ur help.bcoz i want to know about how to sort
the link list using c language.can u help me?

Good luck using his help.
 
C

CBFalconer

.... snip ...

hey thanks ur sorce code.i also think that is correct.then u know
about linklist,i want ur help.bcoz i want to know about how to sort
the link list using c language.can u help me?

ur was a city in ancient times. I (capitalized) is the first
person pronoun. u has not posted here for several years. bcoz is
totally unrecognizable. Periods are normally followed by at least
one blank. First characters in sentences are usually upper case.
Other (possibly typos) errors are: "linklist" != "link list";
"sorce" probably means "source".

Most of this is not a language problem, but pure carelessness. The
result is, to all practical purposes, unintelligible. Please fix
your message before posting.
 
J

James Fang

Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
{
char c;

fflush(stdin); // start reading from stdin
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);

}

I have a question:

If you use the keyboard as the stdin source, how can the situation :
((c = getchar()) == EOF) met?

My perception is that you have to redirect your stdin to a file and
then run this program.

Thanks!
 
S

santosh

James said:
Hi I am new to this forum.

I have taken a class in C some time ago but now I am reading Kernigan
and Richie's book to refresh my knowledge. I think I have forgotten
alot and there are no solutions to the exercises. Maybe some people
here can check through some of my solutions.

Below is a solution to the exercise 1.8.

/* blank counter */

int x, y, z;

void main()
{
char c;

fflush(stdin); // start reading from stdin
while(! feof(stdin) ) {
switch(c = getchar()) {
case ' ' : x++; break;
case '\t': y++; break;
case '\n': z++;
}
}
fclose(stdin); // clean up

printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);

}

I have a question:

If you use the keyboard as the stdin source, how can the situation :
((c = getchar()) == EOF) met?

My perception is that you have to redirect your stdin to a file and
then run this program.

No. Most systems have a key sequence that can signal end-of-file. Under
UNIX it is CTRL-D and under Windows, CTRL-Z. The sequence may have to
appear on it's own line to be effective.
 
R

Richard Heathfield

James Fang said:

If you use the keyboard as the stdin source, how can the situation :
((c = getchar()) == EOF) met?

You have just asked (a very slight variant of) comp.lang.c FAQ 12.1b - see
http://c-faq.com for the answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top