473,698 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2 1.5.4 example programme does not work

this is "word counting" example programme from K&R2 section 1.5.4. it
runs without any error/warning but does not work properly, i.e.

the OUTPUT 1 should be:

NEWLINES: 1
WORDS: 1
CHARs: 5
the OUTPUT 2 should be:

NEWLINES: 4
WORDS: 4
CHARs: 21

--------------- PROGRAMME ---------------
/* K&R2 section 1.5.4

word counting

*/

#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{
int c;
int nc = 0;
int nw = 0;
int nl = 0;

int state = OUT;

while((c = getchar() != EOF))
{
++nc;

if(c == '\n')
++nl;

if(c == ' ' || c == '\t' || c == '\n')
state = OUT;

else if(state == OUT)
{
++nw;
state = IN;
}
}

printf(" NEWLINES: %d\n WORDS: %d\n CHARs: %d\n", nl, nw, nc);

return 0;
}
----------------- OUTPUT 1 -------------
[arch@voodo kr2]$ ./a.out
like
NEWLINES: 0
WORDS: 1
CHARs: 5
[arch@voodo kr2]$

------------ OUTPUT 2 -------
[arch@voodo kr2]$ ./a.out
like this

and this

NEWLINES: 0
WORDS: 1
CHARs: 21
[arch@voodo kr2]$

Mar 20 '07 #1
1 1385
On Mar 20, 6:19 pm, "arnuld" <geek.arn...@gm ail.comwrote:
while((c = getchar() != EOF))
this was the BUG, it should be:

while((c = getchar()) != EOF)

sorry for bothering you

Mar 20 '07 #2

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

Similar topics

6
2157
by: arnuld | last post by:
this is the example programme from that section. it does not print the longest line otherwise it works fine, my version has only 1 extra line of "printf". i want to know why it is not printing the longest line: /* K&R2 section 1.9 character arrays example programme STATEMENT: to take number of lines as input and print the longest line
2
2289
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i found the home
8
4758
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
7
1714
by: arnuld | last post by:
this is the programme which converts a string of digits into its numeric equivalent, given in section 2.7: /* atoi: convert s to integer */ int atoi(char s) { int i, n; n = 0; for (i = 0; s >= '0' && s <= '9'; ++i)
12
2219
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ------------------------------- PROGRAMME -------------------------- /* Section 2.7 type conversions we are asked to write a function "htoi(an array)" that accomplishes this:
16
1801
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to output replacing
19
2398
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
16
1727
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print that input array on terminal. (in "main") 3.) we will reverse the array. (calling "reverse" in "main") 4.) we will print that reversed array. (in "main")
12
1420
by: arnuld | last post by:
there are no compile-time errors or warning. it runs but it does not do what it is supposed to. WANTED: to print the "input C programme" with all comments removed. GOT: does not print anything. -------- PROGRAMME ----------- /* K&R2: exercise 1-23
0
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.