ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"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.
"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
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.
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.