Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help with output

1 view
Skip to first unread message

Alamasy

unread,
Jun 19, 2001, 2:05:31 PM6/19/01
to
Okay, here is what I wanna do:
1) format my output into a column of about 20 spaces in size.
2) justify each line of the column, ei padded out to 20 characters with
extra spaces .

Currently, I've been able to get my output into a column. However my problem
is that I don't know how to justify each line, even more, I want to justify
without breaking up words. Here is an example:

Original text:

After considering the historic page, and viewing the living world with
anxious solicitude, the most melancholy emotions of sorrowful indignation
have
depressed my spirits, and I have sighed when obliged to confess, that either
nature has made a great difference between


Desired Output:


After considering
the historic page,
and viewing the
living world with
anxious solicitude,
the most melancholy
emotions of
sorrowful
indignation have
depressed my
spirits, and I have
sighed when obliged
to confess, that
either nature has
made a great
difference between

Any ideas/hints on how to do this?

Mark A. Odell

unread,
Jun 19, 2001, 2:13:19 PM6/19/01
to
"Alamasy" <ecco...@hotmail.com> wrote in
news:LvMX6.168738$Be4.49...@news3.rdc1.on.home.com:

> Okay, here is what I wanna do:
> 1) format my output into a column of about 20 spaces in size.
> 2) justify each line of the column, ei padded out to 20 characters
> with
> extra spaces .

> Any ideas/hints on how to do this?

With %s you can add things like width and left-justification and right
justification, e.g.

printf("%-35s%35s\n", str1, str2);

--
Warmest regards.

Optimize only if it runs too slowly or does not fit, spaces instead of tabs.

Ben Pfaff

unread,
Jun 19, 2001, 2:09:40 PM6/19/01
to
"Alamasy" <ecco...@hotmail.com> writes:

> Original text:
>
> After considering the historic page, and viewing the living world with
> anxious solicitude, the most melancholy emotions of sorrowful indignation
> have
> depressed my spirits, and I have sighed when obliged to confess, that either
> nature has made a great difference between
>
>
> Desired Output:
>
>
> After considering
> the historic page,
> and viewing the
> living world with
> anxious solicitude,
> the most melancholy
> emotions of
> sorrowful
> indignation have
> depressed my
> spirits, and I have
> sighed when obliged
> to confess, that
> either nature has
> made a great
> difference between

I don't get it. Describe
exactly how you want the
output to appear. The
following is my idea of "full
justification" for text. Your
text above just looks like an
ugly jumble to me.

After considering the historic
page, and viewing the living
world with anxious solicitude,
the most melancholy emotions
of sorrowful indignation have
depressed my spirits, and I
have sighed when obliged to
confess, that either nature
has made a great difference
between

--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield

Pieter Winter

unread,
Jun 19, 2001, 2:19:38 PM6/19/01
to

Alamasy <ecco...@hotmail.com> wrote in message
news:LvMX6.168738$Be4.49...@news3.rdc1.on.home.com...

Sure:

Consider:
- A line contains at least one word
- 2 words on one line have a distance of "at least" one space character
- when a word is larger than a line, break it up.

So
- collect words that fit on a single line
(i.e words' length + 1*(nr of words - 1) < line length)
- amount of white space on one line is
line-length - words' length
- divide the white space evenly to the area's between the words
- output the line
- go back to the start

The last line could be evenly spaced, or justified to the left.

HTH
Pieter

--
Pieter Winter
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-IAQ: http://www.plethora.net/~seebs/faqs/c-iaq.html


Tobias Oed

unread,
Jun 19, 2001, 2:25:24 PM6/19/01
to

Read words and add their lengths (+1 for separating space)
until there is one too many to fit in your 20 spaces. Look
how much the words that fit occupy. Decide how many spaces
to add in between them so the whole thing is justified.
Print these words separated by the spaces just calculated.
Repeat (don't forget the word read that didn't fit on the previous
line!)
HTH Tobias.

Martin Ambuhl

unread,
Jun 19, 2001, 5:19:03 PM6/19/01
to
Alamasy wrote:
>
> Okay, here is what I wanna do:
> 1) format my output into a column of about 20 spaces in size.
> 2) justify each line of the column, ei padded out to 20 characters with
> extra spaces .

After your repeated postings asking us to do your homework, I have given
in. You should be warned that this comes with no guarantees, has not
been carefully checked, and may blow up your machine or perhaps your
grade should you turn it in (and might not even satisfy the real
specifications, which you have refused to give). Here goes:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LLEN 40
int main(void)
{
char original_text[] =


"After considering the historic page, and viewing the living "
"world with anxious solicitude, the most melancholy emotions "
"of sorrowful indignation have depressed my spirits, and I "
"have sighed when obliged to confess, that either nature has "

"made a great difference between";
char buffer[LLEN + 1], *p, *q, *nextp;
int inspace, nonspace, fill, spccnt, gaps;
for (p = original_text; *p;) {
while (isspace(*p))
p++;
if (!*p)
break;
q = p;
if (strlen(p) < LLEN) {
strcpy(buffer, p);
nextp = p + strlen(p);
}
else {
q = p + LLEN;
while (!isspace(*q) && p != q)
q--;
*q = 0;
strcpy(buffer, p);
nextp = p + strlen(p) + 1;
}
q = p + strlen(p);
for (q = p, nonspace = inspace = gaps = 0; *q; q++) {
if (isspace(*q)) {
if (!inspace) {
gaps++;
inspace = 1;
}
}
else {
inspace = 0;
nonspace++;
}
}
if (gaps) {
fill = (LLEN - nonspace) / gaps;
for (q = p, inspace = 0; *q; q++) {
if (isspace(*q)) {
if (!inspace) {
for (spccnt = 0; spccnt < fill; spccnt++)
putchar(' ');
inspace = 1;
}
}
else {
putchar(*q);
inspace = 0;
}
}
putchar('\n');
}
p = nextp;
}
return 0;

Bertrand Mollinier Toublet

unread,
Jun 19, 2001, 7:29:31 PM6/19/01
to
Martin Ambuhl wrote:

> Alamasy wrote:
> >
> > Okay, here is what I wanna do:
> > 1) format my output into a column of about 20 spaces in size.
> > 2) justify each line of the column, ei padded out to 20 characters with
> > extra spaces .
>
> After your repeated postings asking us to do your homework, I have given
> in. You should be warned that this comes with no guarantees, has not
> been carefully checked, and may blow up your machine or perhaps your
> grade should you turn it in (and might not even satisfy the real
> specifications, which you have refused to give). Here goes:
>

> <snip code and output>

You liar ! You were actually intrigued by the exercise and gave a try at it
(by the way, I envy the free time you seem to have : ). Finding out you had
something working, you thought (as a good hacker) : "But, after all, why not
share this code ?"
However, you were afraid we would make fun of your interest in the exercise,
so that you're hiding your attempt behind lassitude at the OP's requests.

Shame on you !

<g>

Martin Ambuhl

unread,
Jun 20, 2001, 12:25:04 AM6/20/01
to
Bertrand Mollinier Toublet wrote:
>
> Martin Ambuhl wrote:
>
> > Alamasy wrote:
> > >
> > > Okay, here is what I wanna do:
> > > 1) format my output into a column of about 20 spaces in size.
> > > 2) justify each line of the column, ei padded out to 20 characters with
> > > extra spaces .
> >
> > After your repeated postings asking us to do your homework, I have given
> > in. You should be warned that this comes with no guarantees, has not
> > been carefully checked, and may blow up your machine or perhaps your
> > grade should you turn it in (and might not even satisfy the real
> > specifications, which you have refused to give). Here goes:
> >
> > <snip code and output>
>
> You liar ! You were actually intrigued by the exercise

Bullshit. I was intrigued by writing a text formater back in 1966. The one I
produced then was a good deal more sophisticated than this throw away piece of
code.

> and gave a try at it
> (by the way, I envy the free time you seem to have : ).

This quick&dirty was indeed quick. It took less than 5 minutes. If you don't
have 5 minutes, I suggest you slow down and take a breath.

> Finding out you had
> something working, you thought (as a good hacker) : "But, after all, why not
> share this code ?"

You fail as a psychologist. You fail at ESP. Your "wit" is not even good
enough to be a bad joke.

> However, you were afraid we would make fun of your interest in the exercise,
> so that you're hiding your attempt behind lassitude at the OP's requests.

Idiot.

Bertrand Mollinier Toublet

unread,
Jun 20, 2001, 11:59:33 AM6/20/01
to
Martin Ambuhl wrote:

> Bertrand Mollinier Toublet wrote:
> >
> >
> > You liar ! You were actually intrigued by the exercise
>
> Bullshit. I was intrigued by writing a text formater back in 1966. The one I
> produced then was a good deal more sophisticated than this throw away piece of
> code.
>
> > and gave a try at it
> > (by the way, I envy the free time you seem to have : ).
>
> This quick&dirty was indeed quick. It took less than 5 minutes. If you don't
> have 5 minutes, I suggest you slow down and take a breath.
>
> > Finding out you had
> > something working, you thought (as a good hacker) : "But, after all, why not
> > share this code ?"
>
> You fail as a psychologist. You fail at ESP. Your "wit" is not even good
> enough to be a bad joke.
>
> > However, you were afraid we would make fun of your interest in the exercise,
> > so that you're hiding your attempt behind lassitude at the OP's requests.
>
> Idiot.
>
> >
> > Shame on you !
> >
> > <g>

Raah ! Do you have to be that agressive ?

Ok, you didn't want to get the joke, after all, why not ? But do you need to flame
me for that ?

And anyway, behind the "joke" is still my incredulity about your :

> After your repeated postings asking us to do your homework, I have given in.

People (among which you are) keep their time repeating that c.l.c. is not
please.do.my.homework, that it does not serve a poster to do its homework for him,
etc, and you provide this guy with this answer to his exercise at his first post. So,
excuse me, but I think I missed something somewhere.

Bertrand

Martin Ambuhl

unread,
Jun 20, 2001, 2:59:05 PM6/20/01
to
Bertrand Mollinier Toublet wrote:

> you provide this guy with this answer to his exercise at his first post.

This is a lie. Crawl back under your rock.

Bertrand Mollinier Toublet

unread,
Jun 27, 2001, 7:03:53 PM6/27/01
to
Martin Ambuhl wrote:

Martin,

I would have liked to take this off c.l.c. In this effect I wrote to you
directly last week. I guess my message was lost to your trash among other
spam, for I haven't received any answer from you since this time.

So, please let me state again that :
- I do not understand the agressivity you're showing towards me in this
thread.
- having only received insults from you, I can only guess that I have
offended you in a way that still escapes me. If this is so, please believe
that it was (and I would like to add "obviously" : I think I deserve not to be
considered an asshole until I have proven to be one) not my intention to do
so.
- still, if the above point is true, I would like you to indicate me how I
have so, rather than flaming me as you have done, and leaving me no clue
whatsoever what was wrong.

Thank you

Bertrand


Martin Ambuhl

unread,
Jun 27, 2001, 7:34:42 PM6/27/01
to
Bertrand Mollinier Toublet wrote:

>
> I would have liked to take this off c.l.c. In this effect I wrote to you
> directly last week.

I suspect that you are seeing some of the week-later repostings that are
cropping up a lot these days. You are responding to old postings.

> I guess my message was lost to your trash among other
> spam, for I haven't received any answer from you since this time.

I saw your answer; I thought we were done. It probably would be good to check
the time and date information on postings to which you respond. No one needs
to see any of this again.

Nick

unread,
Jun 28, 2001, 8:29:49 PM6/28/01
to
Wow, the first time I've seen someone talk out a problem before going nuts.

---KnightX---
"There's a first time for everything"

"Bertrand Mollinier Toublet" <b...@c-cube.com> wrote in message
news:3B3A664D...@c-cube.com...

0 new messages