I'm a beginner and I've recently started learning C and here is an example in "The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie" that I don't understand. This program is supposed to count new lines in input and print out the final result. This is the exact same program that is in the book (page 19). The output of it is nothing. I can input forever and it just goes to a new line...
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
If I put the "printf("%d\n", nl)" statement in the body of the if statement, the output would be printed each time on a new line and the value of "nl" wouldn't reset either. it just increments every time I input something and the program wouldn't terminate.
Why doesn't the example work?
int main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
return 0;
}
I ran it on my computer using Code::Blocks and it worked just fine. maybe you just forgot the int before main and return 0 ^^
Related
#include <stdio.h>
#define YES 0
#define NO 0
int main()
{
int c, nl, nc, nw, tab;
nl = 0;
nc = 0;
nw = 0;
tab = NO;
while ((c = getchar()) != EOF)
{
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\t' || c == '\n')
tab = NO;
else if (tab == NO)
{
tab = YES;
++nw;
}
}
printf("NL: %d\nNC: %d\nNW: %d\n", nl, nc, nw);
}
Hey guys, new coder here. Actually, very much new, I just started a few days ago in hopes of broadening my job opportunities. Anyway, with this program, it counts every new line, word and character. For the most part, I understand what is going on but what I don't understand is the new word part. For example, at the top where we #define YES/NO, the number proceeding the word apparently does not matter? I swapped out 1 and 0 for so many other choices and yet still received the same outcome, why is that? Apologies in advance if this is a dumb question, for I myself am a dumb coder. Take care and thank you for your time!
Previous point:
In C a conditional expression will evaluate to false if it is 0 and true if it is any other value, so YES can be 1, 1000, -1000, etc., as long as it is not 0 the conditional expression where it's evaluated will always be true.
As for the question:
For the code to work as expected YES must be 1, or, as per the explanation above, not 0.
In
else if (tab == NO)
{
tab = YES;
++nw;
}
If YES is 0, tab will always be 0 and NW will not be accurate because it will count all the characters except if they are \t, or \n and this is not what program should do, NW should be the number of words.
For instance, for this input:
This is a word I wrote
You'll have:
NL: 1
NC: 23
NW: 17
When it should be:
NL: 1
NC: 23
NW: 6
Setting tab to 1 (YES) serves as a flag to signal that piece of code to only increment nw 1 time in case there is one of those three charaters thus counting the spaces between the words or the newline character one time only, giving you the number of words in the inputed text.
Welcome to this wolrd!you dont need it. I think this should be the right way(I am sorry for my english and my poor explanations):
#include <stdio.h>
int main()
{
int c, nl, nc, nw;
nl = 0;
nc = 0;
nw = 0;
while ((c = getchar()) != EOF)
{
++nc;
if (c == '\n')
++nl;
if (c != ' ' && c != '\t' && c != '\n')
++nw;
}
printf("NL: %d\nNC: %d\nNW: %d\n", nl, nc, nw);
}
I'm reading K&R's book on C and i got to this part where the output would be the number of newlines that you input.I wanted to make it so that it prints out each number corresponding to the amount of newlines typed as the lines are being read.This only outputs the value of nl after F6 or CTRL+Z has been pressed(EOF).Could someone explain to me why?
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
You forgot some brackets. Here's what your code does currently:
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++nl;
}
}
printf("%d\n", nl);
}
Here's what you probably wanted to do based of indentation:
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++nl;
printf("%d\n", nl);
}
}
}
In C, whitespace is mostly ignored. If you want to run multiple statements together in a block, you need to surround that code with brackets {}
The while loop ends only when the character is an EOF character. EOF is a special character that represents the end of the file the program is reading. Since you are reading from the console, the console itself is the file you are reading from but it as no end. However in your system you can send an EOF character to the console by typing F6 or CTRL+Z
Instead if you want to print the number of lines while typing you should change your code like this:
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++nl;
printf("%d\n", nl);
}
}
}
When i run the line counting code in the C programming language book, i do not get any output.ie nothing is returned that reflects the number of lines. The code is below:
#include <iostream>
//This program counts lines in its input
//
int main() {
int c, nl;
nl = 0;
while ((c = getchar())!= EOF )
if (c == '\n')
++nl; //nl = nl +1
printf("%d\n", nl);
}
The output is below:
/home/xxx/xxxx/lineCounting1/cmake-build-debug/lineCounting1
line1
line2
line3
line4
^D
Process finished with exit code 0
I use ctrl+D to stop the execution and get an output. But nothing (except ^D is returned). What am i doing wrong?
this is a C program so we have to include stdio.h instead of iostream.
in the main we have declared 2 variables
data type of variable name "c" should be "char" and nl is a counter which is currently set to 0.
getchar() is a library function which takes only the first character of of the string.
we are assigning the value of getchar to the c. and traversing it till end of file (EOF).
if condition is true then we are entering into the while.
there is a if condition inside the while which states that if(c == '\n') then incrementing the counter (nl) and displays the count.
code will be look like this but still it won't work because it is entering into the infinite loop
we have to assign something to the variable c.
`#include<stdio.h>
//This program counts lines in its input
int main()
{
char c;
int nl;
nl = 0;
printf("Enter the character\n");
while((c = getchar())!= EOF )
{
if (c == '\n')
{
++nl; //nl = nl +1
printf("%d\n", nl);
}
}
}`
Hello everyone I just started learning C through THE c PROGRAMMING LANGUAGE Second Edition
by Brian. W.Kernighnan (ISBN-13: 978-8131704943)
So here is a script which counts the characters, line, words
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
int c, nl, nw, nc, state;
/* c = input, nl = new line, nc = new character, nw = new word, state = (IN/OUT) */
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF)
{
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT)
{
state = IN;
++nw;
}
}
printf(" The number of lines is: %d,\n The number of words is: %d,\n The number of characters is: %d. \n", nl, nw, nc);
}
However I made a script which does the following without the need of defining state IN and OUT
#include <stdio.h>
main()
{
int nw, nl, nc, c ;
nl = nw = nc = c = 0 ;
while ((c = getchar()) != EOF)
{
++nc;
if (c == '\n')
++nl;
else if (c == ' ' || c == '\n' || c == '\t')
++nw;
}
printf("Words:%d \nCharacters:%d \nLines:%d", nw, nc, nl);
}
So what is the difference between these two, why does the author use the state to define IN and OUT ??
[EDIT]
Oh! I see so the script is just to avoid two things :
1. To avoid word count when there are more than one spaces following the word.
2. Secondly my script would count n-2 words I suppose if proper spacing is done.
Which makes the author's script more fullproof.....Is there anything else except these two ??
And thank You for your answes too....
P.S: I'm sorry this is a bit off-topic is it ok to label the question [SOLVED] or is there any other way of doing this ??
Your version is slightly different from his one: in your program, if you have N consecutive spaces, they will be considered as N - 1 words, because for every space you add one to the word count. Also, the last input word won't be considered.
IN literally means "inside a word" and OUT literally means "outside a word". He is tracking the state of the proverbial cursor as he moves through the lines.
I'm having trouble with k&r 1.5.3. Obviously I'm a complete beginner. Below is the code exactly from the book and exactly as I typed it. It compiled fine and runs. It returns characters but just never prints the line count. I'm using ssh into a Ubuntu machine. Can the key on my wife's mac not be interpreted as '\n'?
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++n1;
printf("%d\n", n1);
}
Correct. Mac uses \r as line ending: http://en.wikipedia.org/wiki/Newline
Update your code like this:
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\r') /* use \r for Macs */
++n1;
printf("%d\n", n1);
}
However
When I try to do the same, I have to Ctrl-D to enter an EOF and trigger the program to print the line count.