473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with printing input.

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;
}

Nov 14 '05 #1
10 1803
On 20 Apr 2005 15:01:57 -0700, "Matt" <no******@gmail.com> wrote:
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>>
Nov 14 '05 #2
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 ");

Nov 14 '05 #3
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?

Nov 14 '05 #4
"Matt" <no******@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
"Matt" writes:
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().
Nov 14 '05 #6
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;
}

Nov 14 '05 #7
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ra**********@gmail.com> wrote:
try execute this program ... #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;
}


And if the input is

hey you rama krishna

is your program going to print one word per line?
--
"Never install telephone wiring during a lightning storm." -- Linksys
Nov 14 '05 #8
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 Roberson wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ra**********@gmail.com> wrote:
try execute this program ...

#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;
}


And if the input is

hey you rama krishna

is your program going to print one word per line?
--
"Never install telephone wiring during a lightning storm." --

Linksys

Nov 14 '05 #9
this reply goes to which thread ?????????

ramakrish...@gmail.com wrote:
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 Roberson wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ra**********@gmail.com> wrote:
try execute this program ...

#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;
}


And if the input is

hey you rama krishna

is your program going to print one word per line?
--
"Never install telephone wiring during a lightning storm." --

Linksys


Nov 14 '05 #10
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;
}


Matt wrote:
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;
}


Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Waser | last post by:
Summary: dlgPrint.PrinterSettings.Copies always returns 1 regardless of what the user entered Details: When trying to print a document, I am first presenting the user with the standard...
14
by: beginner10 | last post by:
How can i changhe this code showing how many persons i saved to the file? And it prints pretty much rubbish. Where is the problem? #include <stdio.h> int main() { int i; FILE *data_file;...
4
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing....
4
by: iwdu15 | last post by:
can anybody help me print from a rich text box? i tried the way they showed on the MSDN web page, but it did not work. I am using VB.net 2003...any help would be appreciated
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
14
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error...
8
by: pavan734 | last post by:
Hi I have got a file "file_data.in" that contains the following data: abcdef I have a code that reads character by character & display it. But to my surprise it is printing one character more at...
1
by: Velhari | last post by:
Hi all, I am used prototype concept in javascript. But here i got a problem while printing arrays. Array.prototype.count = function() { Input = this; document.writeln("<br>"); for ( key...
6
by: fido19 | last post by:
Once upon a time, there lived a chimpanzee called Luycha Bandor (aka Playboy Chimp). Luycha was unhappily married to Bunty Mona, a short but cute little lady chimp. Luycha was tall and handsome –...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.