Removing Spaces from String in MIPS

后端 未结 2 1542
鱼传尺愫
鱼传尺愫 2020-12-22 02:02

I am currently trying to write a code that takes in a string and outputs that same string with no spaces. Everything in my code currently works, but the outputs don\'t delet

相关标签:
2条回答
  • 2020-12-22 02:07

    Strings are NULL terminated, you should move that terminating NULL to the new end of your string as well.

    On a side note, you could do the whole thing in place (for ease in C code:)

    #include <stdio.h>
    
    int main() {
      char string1[100];
      fgets (string1, 100, stdin);
      char *inptr = string1; //could be a register
      char *outptr = string1; //could be a register
      do
      {
        if (*inptr != ' ')
          *outptr++ = *inptr;
      } while (*inptr++ != 0); //test if the char was NULL after moving it
      fputs (string1, stdout);
    }
    
    0 讨论(0)
  • 2020-12-22 02:11

    This is another way to remove spaces from the sting and print it back. simply loop the string and ignore spaces.

        .text
    main:
        li $v0, 4       
        la $a0, prompt
        syscall
    
        li $v0, 8       
        la $a0, input
        li $a1, 100
        syscall
    
        move $s0, $a0
    
    loop:
        lb $a0, 0($s0)
        addi $s0, $s0, 1
        beq $a0, 32, loop
        beq $a0, $zero, done    
    
        li $v0, 11
        syscall
    
        j loop
    
    done:
        jr $ra
    
        .data
    prompt: .asciiz "Enter a string: "
    input: .space 100
    
    0 讨论(0)
提交回复
热议问题