delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/31/03:25:42

From: Kevin Dickerson <kevind AT phs DOT mat-su DOT k12 DOT ak DOT us>
Newsgroups: comp.os.msdos.djgpp
Subject: String formatting problems
Date: Wed, 27 Aug 1997 05:25:16 -0800
Organization: Internet Alaska Inc.
Lines: 82
Message-ID: <34042ABC.892782F5@phs.mat-su.k12.ak.us>
NNTP-Posting-Host: penaltybox13.alaska.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I'll just be abrupt. My problem is that I am attempting, rather
unsucessfully, to write a simple text-wrapper. Nothing fancy, just
something that will pump out the text in a big poofy font. (don't ask.
:-) But anyway, the theory is that I pass a string to function
drawDialogue(char *text), and the function takes char *text and
manipulates it fairly simply. It *should* read each character, split it
up into words (seperated by spaces), and then checking the length of the
current line+length of unwritten word, making sure the text doesn't go
out of bounds of the 30 character limit (hey--it's a poofy font :-),
then does the nessessary tasks. Now, my code is fairly rudimentary right
now, so it's pretty small.

Really, my problem lies within a very narrow position, and I can't debug
it because it goes so far as to re-boot my system when I call this
stupid func! Argh. Oh, and sorry if this message seems vague or
erroneous, but I haven't been to sleep in roughly 28 hours. Bad habits
are hard to break, I suppose. :-)

(BTW, text = "Holy crap! This thing actually works! Wow! Amazing!")

void drawDialogue(char *text)
{
   int   l1, nwords, len;
   char  *chr, *words[2000]; // that would be one freakin' huge dialog
box. :-)

   ...

// This is the problem block:
   for (l1=0; l1 < strlen(text); l1++)  // processes text
character-by-character
   {
      sprintf( chr, "%c", text[l1] );   // prints the scanned character
to string chr
      strcat( words[nwords], chr );     // and this line *should*, but
doesn't concatenate the character
                                        // ...on to the word currently
being added to.
      if ( text[l1] == ' ' )            // and if the processed
character is a space, then...
      {
         nwords++;                      // move the word up. The
checking-of-the-bounding-box isn't really
      }                                 // imperitive, because I've
localized the bug to these commands.
   }

   ...

}

"nwords" is a variable containing the current word that chr will be
writing to.
"words[]" is an array of strings that contain words (fairly obvious,
yes?).
"l1" is the for(...) variable.
"chr" is a buffer containing the string format of the character being
read out of *text.

Truth be told, I've worked with just about everything *but* string
manipulation in C, so I'll probably appear fairly inept at it. I worked
on this problem of formatting the text for many, many hours without
finding any really efficient way to do it. Then I decided that I could
try to make a replica of the 30-character box in Perl (using text
instead of a graphic font), and I finished that in about 3 minutes. They
only real difficulty is figuring out how to efficiently seperate a line
of text into words. In Perl, I just used the command
"@text = split / /, $text" which, if you are familiar with Perl,
seperates $text every time it encounters a space into an array of words,
contained in @text. Argh! It's so easy in Perl, but has given me so much
greif (or is that grief?) in C. Geesh... And as usual, thanks in advance
for your help!

-------------------------------
               Kevin Dickerson
   kevind AT phs DOT mat-su DOT k12 DOT ak DOT us
        http://gdc.home.ml.org
===============================
 Webmaster, Palmer High School
   http://phs.mat-su.k12.ak.us
-------------------------------
Even webmasters get stumped. :)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019