Problem with printing input.

M

Matt

Ok, so the exercise is to:

Write a program that prints its input one word per line.

Does this mean so that when I prest ^Z(ctrl-z) to exit the program if I
were to do this:

Hey you

It would do this:

Hey
you

? If so then I am really lost on howto do that, I've tried putchar, but
that doesn't work(mainly cause well it prints it right after I say it,
and only the 1st letter). I'm really sorry for bothering so much, but I
tried to solve this myself before asking, I tried, I failed. Could
someone please help me? The original code(before printing the words on
new lines(just telling how many words)) is this:

#include <stdio.h>

/* print the input one word per time on different lines */

#define IN 1
#define OUT 0

int main(void)
{
int c, state;
char nw;

state = OUT;
nw = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d\n", nw);
return 0;
}
 
B

Barry Schwarz

Ok, so the exercise is to:

Write a program that prints its input one word per line.

Does this mean so that when I prest ^Z(ctrl-z) to exit the program if I
were to do this:

Hey you

It would do this:

Hey
you

? If so then I am really lost on howto do that, I've tried putchar, but
that doesn't work(mainly cause well it prints it right after I say it,
and only the 1st letter). I'm really sorry for bothering so much, but I
tried to solve this myself before asking, I tried, I failed. Could
someone please help me? The original code(before printing the words on
new lines(just telling how many words)) is this:

#include <stdio.h>

/* print the input one word per time on different lines */

#define IN 1
#define OUT 0

int main(void)
{
int c, state;
char nw;

state = OUT;

You should limit the use of state to only indicate if you have output
any letters (to prevent skipping multiple lines if there is more than
one space between two words).
nw = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')

You might want to consider using the isspace function here.
state = OUT;

You also need to print out the character. Consider using putchar.
else if (state == OUT) {
state = IN;
++nw;

You did not mention any requirement to count the number of words. Why
are you counting in a char instead of an int.

You need to output a \n here to complete this word's line.
}
}
printf("%d\n", nw);

Don't you think a title to go with the number would be nice?
return 0;
}



<<Remove the del for email>>
 
S

Steven

I am not sure what you are trying to do, but if you want to break up a
string using delimiters use strtok.
Assuming the string is stored in 'input', you do

first_token=strtok(input,"\n\r ") ;//break up string using white spaces

as delimiters.

the next tokens are called with 'NULL' to the first argument.

second_token=strtok(NULL,"\n\r ");

and keep going until a null is returned.

If you call strtok with a string first argument, you reset the
tokenizer to the new string.

other_token=strtok(otherstring,"\n\r ");
 
M

Matt

I'm not sure. I'm asking how to do what it says in the exercise. That's
the code, more or less, what it gives you in the book, you're sposed to
expand off of it, or something to print what the user types in on a new
line per word. Help, anyone?
 
K

Keith Thompson

Matt said:
I'm not sure. I'm asking how to do what it says in the exercise. That's
the code, more or less, what it gives you in the book, you're sposed to
expand off of it, or something to print what the user types in on a new
line per word. Help, anyone?

You're not sure about what? We can't necessarily see the article to
which you're replying; you need to provide some context.

Google makes it far too easy to make this mistake, but there is a
workaround.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
O

osmium

Matt said:
I'm not sure. I'm asking how to do what it says in the exercise. That's
the code, more or less, what it gives you in the book, you're sposed to
expand off of it, or something to print what the user types in on a new
line per word. Help, anyone?

I think the exercise intends you to read a line at a time instead of a char
at a time. getchar() works on characters, not lines. Find something better
to use instead of getchar().
 
R

ramakrishnat

try execute this program ...
if input is this ...
hey you rama krishna
output:
hey
you
rama
krishna
is that what u want ?

#include <stdio.h>

/* print the input one word per time on different lines */

int main(void)
{
int c;

while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
putchar('\n');
else
putchar(c);

}

return 0;
}
 
R

ramakrishnat

this program assumes that the words in a line are separated only by one
space.
but writing a program that handles variable number of spaces is a
breeze once u know the basic logic.
Walter said:
And if the input is

hey you rama krishna

is your program going to print one word per line?
Linksys
 
R

ramakrishnat

this reply goes to which thread ?????????

this program assumes that the words in a line are separated only by one
space.
but writing a program that handles variable number of spaces is a
breeze once u know the basic logic.

Linksys
 
R

ramakrishnat

try this ...

#include <stdio.h>

/* print the input one word per time on different lines */

int main(void)
{
int c,space_flag=0;

while ((c = getchar()) != EOF)
{
if ((c == ' ' || c == '\t'))
space_flag++;
else if (space_flag != 0)
{
putchar('\n');
putchar(c);
space_flag = 0;
}
else
putchar(c);
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top