How to remove all punctuation and spaces in a string?

二次信任 提交于 2021-02-13 16:35:14

问题


I have input like this:

This is, ,,, *&% a ::; demo + String. +Need to**@!/// format:::::!!! this.`

Output Required:

ThisisademoStringNeedtoformatthis

I have to do this without using str_trim.

Edit: I am writing an encryption program. I have to remove all punctuation from the string and turn all lower case letters to uppercase before I encrypt it.

I added the code. I need to remove the spaces, or any punctuation before I turn it to upper case. So far I haven't found anything in my book that could help with this except str_trim which we aren't allowed to use.

INCLUDE Irvine32.inc

.data
source  byte  "This is the source string",0

.code
main proc


mov  esi,0              ; index register
mov  ecx,SIZEOF source  ; loop counter
L1:
mov  al,source[esi]     ; get a character from source
and  source[esi], 11011111b     ; convert lower case to upper case
inc  esi                ; move to next character
loop L1                 ; repeat for entire string

mov edx, OFFSET source
call WriteString

exit
main endp
end main

回答1:


Your are already trying to change from lowercase to uppercase, so, I will give you a hand to remove the punctuation. Next code uses my suggestion : moving the uppercase letters to an auxiliary string ignoring the punctuation characters. I used EMU8086 compiler :

.stack 100h
.data
source  db  "STRING, WITH. PUNCTUATION : AND * SPACES!$"
aux     db  "                                          "
.code
  mov  ax, @data
  mov  ds, ax

;REMOVE EVERYTHING BUT UPPERCASE LETTERS.

  mov  si, offset source   ; POINT TO STRING.
  mov  di, offset aux      ; POINT TO AUXILIARY.
L1:
  mov  al, [ si ]          ; get character from source
;CHECK IF END STRING ($).
  cmp  al, '$'
  je   finale
;CHECK IF CHAR IS UPPERCASE LETTER.
  cmp  al, 65
  jb   is_not_a_letter    ; CHAR IS LOWER THAN 'A'.
  cmp  al, 90
  ja   is_not_a_letter    ; CHAR IS HIGHER THAN 'Z'.
;COPY LETTER TO AUX STRING.
  mov  [ di ], al
  inc  di                ; POSITION FOR NEXT CHARACTER.
is_not_a_letter:
  inc  si                ; move to next character
  jmp  L1

finale:
  mov  [ di ], al        ; '$', NECESSARY TO PRINT.

;PRINT STRING.  
  mov  dx, OFFSET aux
  mov  ah, 9
  int  21h

;END PROGRAM.
  mov  ax, 4c00h
  int  21h              

I ended the strings with '$' because I print the string with int 21h.

As you can see, I'm not using CX nor the LOOP instruction. What I do is to repeat until '$' is found. You can do the same until 0 is found.




回答2:


This is my code after removing all the punctuation and turning it to uppercase.

INCLUDE Irvine32.inc

.data
source  byte  "STriNG, @# WITH.[][][ lalalala PUncTuATION : AND * SpaceS!", 0
target  byte  SIZEOF source DUP(0), 0                                           

.code
main PROC

    pushad

    mov edx, offset source
    call WriteString
    call CrlF
    mov edx, 0

    mov  esi, offset source 
    mov  edi, offset target      
L1:
    mov  al, [ esi ]          ; get character from source

    cmp  al, 0
    je   final

    cmp  al, 65
    jb   not_letter    ; if char is lower than 'A' jump to not letter
    cmp al, 122
    ja  not_letter     ; if char is greater than 'z' jump to not letter

    cmp al, 90
    ja Label1           ; jump if above 'Z'
    jmp next            ; false
    Label1:
    cmp al, 97
    jl Label2           ; jmp if less than 'a'
    jmp next            ; false
    Label2:             ; if both are true than is greater than 'Z' but less than 'a'
    jmp not_letter      ; jump to not letter

    next:
    mov  [ edi ], al
    inc  di                ; position to next character.

    not_letter:
    inc  si                ; move to next character
    jmp  L1

    final:
    mov  [ edi ], al     

    mov  edx, OFFSET target
    mov  ah, 9
    call WriteString
    call CrlF

    mov  esi,0              ; index register
    mov  ecx,SIZEOF source  ; loop counter
    L2:
    mov  al, target[esi]     ; get a character from source
    and  target[esi], 11011111b     ; convert lower case to upper case
    inc  esi                ; move to next character
    loop L2                 ; repeat for entire string

    mov edx, OFFSET target
    call WriteString
    call CrlF

    popad

  exit
  main endp
  end main


来源:https://stackoverflow.com/questions/36721559/how-to-remove-all-punctuation-and-spaces-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!