LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Exercise 1-12 in the K&R - Problem with stdout. (https://www.linuxquestions.org/questions/programming-9/exercise-1-12-in-the-k-and-r-problem-with-stdout-4175681128/)

Arct1c_f0x 08-27-2020 11:51 AM

Exercise 1-12 in the K&R - Problem with stdout.
 
"Exercise 1-12. Write a program that prints its input one word per line."

I'm trying to figure out what stupidly obvious thing I've overlooked, lend me a hand?

Quote:

#include <stdio.h>

// This program will print its input one word per line

#define IN 1 //
#define OUT 0 //


int main() {

int c, word, state;

state = OUT;

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

if (c != ' ' || c != '\t' || c != '\n') {
state = IN;
word += c;
}
if (c == ' ' || c == '\t' || c == '\n') {
state = OUT;
printf("%d\n", word);
word = 0;
}

}
}
Here's what I came up with but my program would only print numbers instead of printing single words. Earlier in the book it says that you can delcare an int variable and then use it to store strings as long as the numbers within the variable are interpreted back into characters before being displayed to StdOut. Did it really say that or am I dellusional? I can't find it now. What did I do wrong? I tried replacing the printf() statement with put char but then I ended up with even stranger output! everytime I would type an 'h' it would display an 'r' to stdout, and everytime I would type a random letter it would print some other random letter different from the one that I typed. I'm going to keep trying to figure out what I did wrong but I'm going to check back to see what you guys have to say periodically.

My humble gratitude,

Arct1c_f0x 08-27-2020 11:58 AM

Quote:

Originally Posted by Arct1c_f0x (Post 6160109)
"Exercise 1-12. Write a program that prints its input one word per line."

I'm trying to figure out what stupidly obvious thing I've overlooked, lend me a hand?



Here's what I came up with but my program would only print numbers instead of printing single words. Earlier in the book it says that you can delcare an int variable and then use it to store strings as long as the numbers within the variable are interpreted back into characters before being displayed to StdOut. Did it really say that or am I dellusional? I can't find it now. What did I do wrong? I tried replacing the printf() statement with put char but then I ended up with even stranger output! everytime I would type an 'h' it would display an 'r' to stdout, and everytime I would type a random letter it would print some other random letter different from the one that I typed. I'm going to keep trying to figure out what I did wrong but I'm going to check back to see what you guys have to say periodically.

My humble gratitude,


Here's a sample of the output that I got after compiling it and running it in my bash terminal. "do you know where I am going?" is the input I supplied to the program

Code:

do you know where I am going?
243
381
479
571
105
238
605


michaelk 08-27-2020 12:35 PM

Code:

word += c;

do you know where I am going?
243
381
479
571
105
238
605

This is adding the ASCII value of each word plus the following space.

d + o + space = 100 + 111 + 32 = 243

It is true that you can print the character of an integer as represented by the ASCII value but you can not append them together in this fashion. One fix would be to output the character as received on the same line until a space or EOF.

NevemTeve 08-27-2020 12:41 PM

Variable 'int word' cannot store a word. Actually, you don't have to store the word, it's enough to write its chacarters to stdout as soon as you read them.

Arct1c_f0x 08-27-2020 01:37 PM

Thanks guys! Now it does what it's supposed to.
I cut out all the extra nonsense I had put into the program because it simple wasn't necessary.

Code:


#include <stdio.h>

// This program will print its input one word per line


int main() {

int c;


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

    if (c != ' ' || c != '\t' || c != '\n') {
        putchar(c);
    }
    if (c == ' ' || c == '\t' || c == '\n') {
        putchar('\n');
    }
}
}

[Output]
hey how's int going, man?
hey
how's
int
going,
man?
[/Output]



While I have you guys here could you briefly explain what the difference between single and double quotes is in C? I've only ever learned Python and JavaScript before and as you guys probably know, the two are, at least to my knowledge, completely interchangable. But it seems that I've run into issues with using '' and "" as if they were the same. Is it that one set interprets what's inside them literally or numerically and the other doesn't?



Thanks again you guys.

NevemTeve 08-27-2020 02:42 PM

The first if could be
Code:

if (c != ' ' && c != '\t' && c != '\n') {

Arct1c_f0x 08-27-2020 02:48 PM

Quote:

Originally Posted by NevemTeve (Post 6160148)
The first if could be
Code:

if (c != ' ' && c != '\t' && c != '\n') {

You're totally right. Strange though because when I tested it with all OR statements it worked as intended. but the logic of using AND statements is better.

dugan 08-27-2020 04:51 PM

You can use the strtok library function, if you don't think it would defeat the point of the exercise.

rtmistler 08-27-2020 05:16 PM

Quote:

Originally Posted by Arct1c_f0x (Post 6160109)
"Exercise 1-12. Write a program that prints its input one word per line."

Excuse me?

Is this not as simple as grabbing the argument list and outputting those arguments?

I.e. Non-compiled/tested, coded by seat of pants:
Code:

int main(int argc, char **argv)
{
    if(argc > 1) {
        for(int i = 1; i < argc; i++) {
            printf("%s\n", argv[i]);
        }
    }
}


dugan 08-27-2020 05:45 PM

I assume you're supposed to get the "input" with scanf or something similar.

rtmistler 08-27-2020 07:50 PM

Quote:

Originally Posted by dugan (Post 6160186)
I assume you're supposed to get the "input" with scanf or something similar.

Maybe. Exercise 1 dash something seems early on to me.

Maybe not enough of the problem was related.

AnanthaP 08-27-2020 10:19 PM

Quote:

I assume you're supposed to get the "input" with scanf or something similar.
K&R 1 st edition had something like "a surprising amount of i/o can be done with redirection".

Just dug out my old K&R first edition and will supply the reference.

Or perhaps it was in THE UNIX PROGRAMMING ENVIRONMENT by Kernighan and Pike.

AP

AnanthaP 08-27-2020 10:24 PM

Quote:

"Exercise 1-12. Write a program that prints its input one word per line."
The redirectional opposite of getchar is clearly putchar.

AP

GazL 08-28-2020 06:30 AM

I agree with AP. Been a while, but from what I remember, the early chapters of the K&R book were all about throwing characters around.

michaelk 08-28-2020 07:04 AM

To answer your second question, a single quote is used to identify a single character and double quotes are used for string literals. A string literal “x” is a string, containing the character ‘x’ and a null terminator ‘\0’. So “x” is two-character array in this case.


All times are GMT -5. The time now is 06:42 AM.