2017-03-01 1 views
1

현재 문자열을 받아 공백없이 동일한 문자열을 출력하는 코드를 작성하려고합니다. 내 코드의 모든 것은 현재 작동하지만 출력은 문자열의 이전 값을 삭제하지 않습니다. 예를 들어, 입력이 "잭이고 jill이 언덕 위로 뛰어"갔다면 산출량은 "jackandjillranupthehill hill"입니다. 내 문자열은 여전히 ​​오래된 가치를 고수하고있는 것 같습니다. 누구나 이것이 왜 이렇게하고 어떻게 고쳐야하는지 알 수 있습니까?MIPS에서 문자열에서 공백 제거

.data 
string1: .space 100 
string2: .space 100 
ask: .asciiz "Enter string: " 
newstring: .asciiz "New string:\n" 

.text 

main: 
la $a0,ask #ask for string1 
li $v0,4 
syscall 

#load String1 
la $a0,string1 
li $a1, 100 
li $v0,8 #get string 
syscall 


load: 
la $s0,string1 #Load address of string1 into s0 
lb $s1, ($s0) #set first char from string1 to $t1 
la $s2 ' ' #set s2 to a space 
li $s3, 0 #space count 


compare: 
#is it a space? 
beq $s1, $zero, print #if s1 is done, move to end 
beq $s1, $s2, space #if s1 is a space move on 
bne $s1, $s2, save #if s1 is a character save that in the stack 

save: 
#save the new string 
sub $s4, $s0, $s3, 
sb $s1, ($s4) 
j step 

space: 
addi $s3, $s3, 1 #add 1 if space 

step: 
addi $s0, $s0, 1 #increment first string array 
lb $s1, ($s0) #load incremented value 
j compare 



print: 
#tell strings 
la $a0, newstring 
li $v0,4 
syscall 

#print new string 
la $a0, string1 
li $v0, 4 
syscall 

end: 
#end program 
li $v0, 10 
syscall #end 

답변

0

문자열이 NULL가 종료됩니다, 당신은 이동해야 그뿐만 아니라 당신의 문자열의 새로운 끝으로 NULL을 종료.

보조 노트에

, 당신은 C 코드에서 쉽게 장소 (에서 모든 일을 할 수있는 :

#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

이것은 고통에서 공백을 제거하고 다시 인쇄하는 또 다른 방법입니다. 단순히 문자열을 반복하고 공백을 무시하십시오.

.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