News:

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

removing commas from a string?

Started by jckl, March 05, 2006, 04:43:11 AM

Previous topic - Next topic

jckl

how would i go about removing commas from a string. I can add commas but cant figure out how to remove them.

donkey

Start scanning the string at the beginning, no need for a second buffer the resulting string will never be longer than the original. When you encounter a comma shift the remainder of the string one place to the left (overwriting the comma) and continue scanning until you reach the end of the string.

This should work...

// Strip commas
mov edi,offset String
dec edi
:
inc edi
mov al,[edi]
test al,al
jz >.EOS
cmp al,","
jne <
mov esi,edi
.SHIFT
// shift the string
inc esi
mov al,[esi]
mov [esi-1],al
test al,al
jnz <.SHIFT
dec edi
jmp <
.EOS
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

donkey

I guess I should post MASM syntax too...

; Strip commas
mov edi,offset String
dec edi
HUNT:
inc edi
mov al,[edi]
test al,al
jz SHORT EOS
cmp al,","
jne  SHORT HUNT
mov esi,edi
SHIFT:
; shift the string
inc esi
mov al,[esi]
mov [esi-1],al
test al,al
jnz SHORT SHIFT
dec edi
jmp SHORT HUNT
EOS:
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jckl

awww i was somewhat on the right track. Thanks for the help and it works like a charm :)

raymond

Actually, there is no need to shift all of the remaining string everytime you encounter a coma. You could do it also this way, assuming a null-terminated string:

  mov edi,offset String
  mov esi,edi
toploop:
  lodsb           ;get character
  cmp al,","      ;is it a coma
  jz  @F          ;disregard if it is
  stosb           ;store character
@@:
  test al,al      ;is it end of string
  jnz toploop     ;continue until end of string
;the string is now free of comas and null-terminated


Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

usafman2006

What about removing multiple items from a string?  For example, say you have the string: "Hello! I'm really hungry!"  How would you go about removing anything that wasn't either an uppercase character or number (commas, spaces, and any other punctuation or special character)?

dedndave

well - to make it faster....

i would not modify the original string
instead, make a new string in an empty buffer
copy only the characters you want and skip the ones you do not want

usafman2006

Ahhh, thanks!  I'll try that instead

dedndave

i guess it isn't going to be any faster   :P
but, it may be desirable to retain the original string, anyways

write the routine so that it accepts an input buffer address and an output buffer address...
then, make it so they can be the same
that way, the caller can choose to overwrite the original string, if desired

ToutEnMasm

an another style

Quote
   Lea edx,string
   @@:
   .if byte ptr [edx] == 0
      jmp @F
   .elseif byte ptr [edx] == ","
      mov byte ptr [edx]," "    ;or other char
   .elseif byte ptr [edx] == "Other"
      mov byte ptr [edx],"other char"
   .endif
   inc edx
   jmp @B
   @@:

usafman2006

I need to keep the original string in tact so I can use it as part of the output.  I think writing a procedure to keep the characters I want will be much easier than writing one that deletes the ones I don't want.  I'm basically writing a program to evaluate if a string is a palindrome.  So, I need to keep only letters and numbers, convert all letters to either uppercase or lowercase, and compare the new modified string to itself... one being front to back and the other being back to front.  At least that's my thoughts on it.

ToutEnMasm

Quote
   Lea edx,string
   lea ecx,output
   @@:
   .if byte ptr [edx] == 0
      jmp @F
   .elseif byte ptr [edx] >= "a" && byte ptr [edx] <= "z"
      mov al,byte ptr [edx]
      mov byte ptr [ecx],al    ;or other char
      inc ecx
   ;...
   .endif
   inc edx
   jmp @B
   @@:
   mov byte ptr [ecx],0

hutch--

Do both, use 2 buffers if you need the original, overwrite it if you don't. If you need to handle multiple characters, use a BYTE table.


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    proc_no_commas PROTO src:DWORD,dst:DWORD

    nocommas MACRO arg1
      invoke proc_no_commas,arg1,arg1
      EXITM <eax>
    ENDM

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL str1  :DWORD

    mov str1, chr$("This, is, a, test,,.,")

    mov str1, nocommas(str1)

    print str1,13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

proc_no_commas proc src:DWORD,dst:DWORD

    mov ecx, [esp+4]        ; src
    mov edx, [esp+8]        ; dst

  @@:
    movzx eax, BYTE PTR [ecx]
    add ecx, 1
    cmp eax, ","
    je @B
    mov [edx], al
    add edx, 1
    test eax, eax
    jnz @B

    mov eax, [esp+8]        ; return the dst address for the macro.

    ret 8

proc_no_commas endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

NoCforMe

Palindromes, eh? Interesting.

Not an answer to your question, but the attached file has a little program that lets you play around with palindromes with an edit box. Might give you some ideas. It's something I've been thinking about for a long time, a palindrome-making program.

"Straw? No, too stupid a fad: I put soot on warts!"

usafman2006

on a side note, anyone know how to concatenate strings together?  I'm trying to put three strings together to form one long string for my final output. 
For example:

String1 byte 'The string ',0
String2 byte ' is a palindrome.',0
String3 byte ' is not a palindrome.',0

I need to concatenate string1 with the original string the user entered, then concatenate that string with either string2 or string3
so the final string should look something like this:

The string "taco cat" is a palindrome.