473,386 Members | 1,738 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,386 software developers and data experts.

Counting blanks

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);
}
Nov 30 '07 #1
13 1940
ra****@thisisnotmyrealemail.com wrote:
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 }

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Nov 30 '07 #2
ra****@thisisnotmyrealemail.com wrote:
>
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 */

--
pete
Nov 30 '07 #3
ra****@thisisnotmyrealemail.com wrote, On 30/11/07 21:54:
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.
--
Flash Gordon
Nov 30 '07 #4
On Nov 30, 1:54 pm, raj...@thisisnotmyrealemail.com wrote:
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;
}
Nov 30 '07 #5
ra****@thisisnotmyrealemail.com wrote:
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!

--
Ian Collins.
Nov 30 '07 #6
ra****@thisisnotmyrealemail.com wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Nov 30 '07 #7
On Fri, 30 Nov 2007 13:54:11 -0800 (PST),
ra****@thisisnotmyrealemail.com wrote in comp.lang.c:
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
Dec 1 '07 #8
On Dec 1, 2:54 am, raj...@thisisnotmyrealemail.com wrote:
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?
Dec 1 '07 #9
ab*********@gmail.com wrote:
On Dec 1, 2:54 am, raj...@thisisnotmyrealemail.com wrote:
>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.

Dec 1 '07 #10
ab*********@gmail.com wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #11
On Dec 1, 5:54 am, raj...@thisisnotmyrealemail.com wrote:
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!
Dec 2 '07 #12
James Fang said:

<snip>
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 2 '07 #13
James Fang wrote:
On Dec 1, 5:54 am, raj...@thisisnotmyrealemail.com wrote:
>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.

Dec 2 '07 #14

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

Similar topics

1
by: Andreas Lauffer | last post by:
I use Blanks in ColumnNames ( I know that this isn´t very good, but a lot of code and querys had to be changed if I would remove all blanks in all columnnames). When I link this tables with ODBC...
6
by: jalkadir | last post by:
I am trying to find a way to remove the leading and tailing blanks from a string. What I have come up with is not doing the work as I expected; because if the value I pass to the function is "...
2
by: William Kossack | last post by:
I have an Access table with one primary key and am attempting to update a non-key field, using UPDATE tblMethtest SET fev1timemeth = '' WHERE SID = '0041R'; When I do this, the field...
4
by: Matt | last post by:
Ok, sorry for posting so much, but I just have alot of questions. Ok, so It says to make a program to count blanks, tabs, and newlines. So I made one: #include <stdio.h> /* Count tabs,...
2
by: DaFerg | last post by:
Hi ... I have a weblog database where I want to count the occurences of a table of string values that appear in all the urls viewed. My tblWebLog as a field that contains the url ... tblWebLog....
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
10
by: Diego F. | last post by:
Hi all. I have an application that receives a message from a socket in an array from a certain size. As the array size may be longer that the message received, the end of the array has blank...
2
by: chenxinhlj | last post by:
Description a hero invented a new style of cross-bow that could shoot consecutively. The arrow could hit the eagle exactly if only the arrow could reach the height of the eagle. However, there was...
4
by: eBob.com | last post by:
I have a RichTextBox in which I'd like blanks to appear different from nothing. Imagine a file which does not fill up the RTB. You can't tell how many, if any, blanks might follow the last...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.