News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

String Manipulation and Formatting

Started by newbieinfl, April 19, 2006, 03:23:27 PM

Previous topic - Next topic

newbieinfl

Hi all again-


I wrote a little program that asks a user for input (name, address, phone numbers, etc.) and I have a little problem with the name formatting.  Even though I specify the format for the name as first name last name, some users are putting in last name, first name.  At first I tried just stripping out the comma but I really need to strip it out and reverse the names.  I have been able to scan for the comma and put the first name into a new string but I can't seem to get both the first name (space) last name into one string.  Can anyone please help?  Thanks.


JR

Tedd

If you're taking the first name as being substring(n+2,string_length) [n being the position of the comma; and followed by a space]
then the last name will be substring(0,n-1)
No?

It's probably easier to extract both names from the user-input, store them separately, and then do whatever you like with them after you know where they are :wink
No snowflake in an avalanche feels responsible.

Mark Jones

What Tedd says makes sense. Perhaps try something like this pseudo-logic:


1. copy character into destination string1
2. was it a comma, space, or null?
3. if not, goto 1                                      ; copies first word
  4a. if comma, make a flag (byte, reg, stack, etc.)
  4b. if null, exit
  4c. if space,
    5. copy next character to dest string2
    6. was char a null?
    7. if not, goto 5                                  ; copies second word
8a. if flag set, switch string1 and string2 offsets.
8b. else don't switch string offsets. :)


Look up the definition for the STOSB/W/D mnemonics (if you haven't already), they are ideal for this type of byte-parsing. Load ESI and EDI with the source and destination string offsets, like so:


; allocate buffers for source and destination
    stralloc TextLen                        ; allocate source memory sizeof text
    mov hMemSrc,eax
    mov esi,eax                             ; esi = source offset
   
    mov eax,TextLen
    shl eax,2                               ; multiply size * 4
    stralloc eax
    mov hMemDst,eax                         ; dest buffer
    mov edi,eax                             ; edi = dest buffer


And don't forget to free buffer pointers with strfree. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

zooba

If you're specifying 'firstname lastname' and the users are getting it wrong, why is it up to you to handle that? When the output doesn't look right they'll change it and mend their ways  :bdg

GregL

newbieinfl,

Use separate inputs for first and last name.