Jump to content

Text justification algorithm/structure help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
geon

geon

    Newbie

  • Members
  • PipPip
  • 24 posts
Hi all...

I have to write a program to type-justify a text file for one of my classes...easy stuff really...I was wondering if anybody could give me a little help designing my algorithm and data structure though (I am drawing a blank on how to setup the spacing).

My plan is to use a linked list and read in the text one paragraph at a time. I will strip the spaces as I scan in the text and make the list's nodes words and punctuation (separated).

My problem comes with the space padding, and doing it efficiently and with as compact code as possible...I'm lazy so sue me lol.

I was thinking, perhaps, of keeping track of the number of spaces needed on the line as the number of words to print on the line is calculated and to add however many 'padding spaces' I will need to justify the line, and then just cycle over the line adding a space in every gap until the number of spaces used is reach. (of course I will account for double spacing after periods).

Any better ideas on this. I can't help but think there is a better data structure or spacing algorithm...or maybe at least one that's easier to code.


Sorry for the long long long post...I'm sure there will be a fair share of tldnr's lol...

But Thanks!!!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm not clear on what you're after:
1) Full-justify the text in a file
2) Full-justify the text sent to a printer
3) left/right justify the text in a file
4) left/right justify the text sent to a printer
5) something else?

When printing, you will need to be aware of things like font. All of this will probably depend heavily on the language/library you are using. Some of these are not possible with certain languages. In other cases, it's simply an option in a component.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
geon

geon

    Newbie

  • Members
  • PipPip
  • 24 posts
Ya, I suppose I could have been a little more clear huh?

It's simply just output to screen the text "type-justified". I'm not all too familiar with the justify alignment but It's where the left and right margins are flush...ie start at same spot and end at same spot.

And it is being written in C. The text is simple ascii so that makes it a bunch easier. Font isn't something needed to be worried about.

Like I said, fairly straight-forward mundane program. I just can't decide/figure out how I want to approach it...and I wanted some input before I started coding.

Thanks again :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Ok, you're talking about full-justification.

What I would do is read one line at a time into a string.
Scan through the string counting the number of spaces in it.
Compare this with the number of spaces needed to pad the string out to the standard length.
This will give you the number of spaces to include at each space. Obviously, sometimes you'll have to add more than other times. I would do some sort of even distribution of the extras (maybe track in a float and an int for how many need to be added/have been added and add the integer difference?).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
geon

geon

    Newbie

  • Members
  • PipPip
  • 24 posts
Hmm...Can't read it in one line at a time because there is a variable margin(char's per line)...

But you def just helped me in my pre-coding designing because I didn't even think about evenly distributing the spaces...lol I didn't even think about how stupid it would look if the padding always started at the same location's...although I'm sure he doesn't care if we do it I'll still prob try to find the time to get that in there (I like to go above and beyond and not take shortcuts, even with these stupid pointless mundane school assignments...it makes me a better programmer :))

Thanks for the help!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Just read the text character by character into a char array until you hit a newline. Then process, then repeat.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog