LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2020, 11:51 AM   #1
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Rep: Reputation: 24
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,
 
Old 08-27-2020, 11:58 AM   #2
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Arct1c_f0x View Post
"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
 
Old 08-27-2020, 12:35 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
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.
 
1 members found this post helpful.
Old 08-27-2020, 12:41 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
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.
 
2 members found this post helpful.
Old 08-27-2020, 01:37 PM   #5
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
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.
 
Old 08-27-2020, 02:42 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
The first if could be
Code:
if (c != ' ' && c != '\t' && c != '\n') {
 
Old 08-27-2020, 02:48 PM   #7
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 08-27-2020, 04:51 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,200

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
You can use the strtok library function, if you don't think it would defeat the point of the exercise.
 
Old 08-27-2020, 05:16 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Arct1c_f0x View Post
"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]);
        }
    }
}
 
Old 08-27-2020, 05:45 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,200

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
I assume you're supposed to get the "input" with scanf or something similar.
 
Old 08-27-2020, 07:50 PM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by dugan View Post
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.
 
Old 08-27-2020, 10:19 PM   #12
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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

Last edited by AnanthaP; 08-27-2020 at 10:22 PM.
 
1 members found this post helpful.
Old 08-27-2020, 10:24 PM   #13
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

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

AP
 
1 members found this post helpful.
Old 08-28-2020, 06:30 AM   #14
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,882

Rep: Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988
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.
 
Old 08-28-2020, 07:04 AM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar - write to stdout & create log files ( from stdout and stderr ) [solved] paziulek Linux - General 2 02-23-2014 12:26 PM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration