Computer Science Canada help with text |
Author: | viper012289 [ Sun Feb 19, 2006 3:32 pm ] |
Post subject: | help with text |
hey i was wondering how i would centre text in file. i managed to get the text centred into the run window but when i check the text file its still aligned right...help would be appreciated var streamnumber : int var streamnumber2 : int var words : string var i : int open : streamnumber, "text.txt", get open : streamnumber2, "centre.txt", put i := 1 loop get : streamnumber, words : * put : streamnumber2, words locate (i, maxcol div 2 - length (words) div 2) put words i := i + 1 exit when eof (streamnumber) end loop close : streamnumber2 |
Author: | Cervantes [ Sun Feb 19, 2006 3:43 pm ] |
Post subject: | |
Since a file doesn't have a "maxcol", the best thing you could do is go through all the lines and find the longest line. Set things so that this line will, when everything is "centred", just touch the left margin (ie. don't add spaces to this line). To "centre" other lines, pad them with spaces. |
Author: | viper012289 [ Sun Feb 19, 2006 4:05 pm ] |
Post subject: | |
how exactly would i go about doing that. |
Author: | Cervantes [ Sun Feb 19, 2006 4:22 pm ] |
Post subject: | |
You'll learn more if you do it yourself than if someone tells you the answer. Give it a try. You may find the String Manipulation Tutorial of help. |
Author: | viper012289 [ Sun Feb 19, 2006 4:33 pm ] |
Post subject: | |
i managed to calculate how much i will be spacing by counting the characters in each line and then subracting it from the largest line in my case 70....but i'm not sure how to place it in the put line with the file... |
Author: | Cervantes [ Sun Feb 19, 2006 4:38 pm ] | ||
Post subject: | |||
viper012289 wrote: but i'm not sure how to place it in the put line with the file...
|
Author: | viper012289 [ Sun Feb 19, 2006 4:51 pm ] |
Post subject: | |
thanks so much it worked like a charm....only one question....sometimes when centreing the number of spaces technically is supposed to be 2.5 so my question is does it actually space 2.5 from the edge or does it just round and go to 3? |
Author: | Cervantes [ Sun Feb 19, 2006 5:01 pm ] |
Post subject: | |
It would put 2 or 3 spaces. Unless your font has a "half a space" character, it's impossible to have half a space. If you're using a monospaced font, it's impossible. |
Author: | sylvester-27 [ Mon Feb 20, 2006 8:43 am ] |
Post subject: | |
just for reference, use code tags when posting code |