спросите пользователя, какую строку он хочет заменить - PullRequest
0 голосов
/ 09 марта 2020

Я пытался написать программу mips, в которой пользователь должен ввести строку и параметр для замены символа, который он хочет заменить внутри строки. Я был в состоянии написать программу, чтобы попросить пользователя ввести строку и отобразить, но я не мог понять, как проверить строку и заменить ее, вот мой код. можем ли мы использовать if else, чтобы проверить, является ли пользовательский ввод положительным или нет, и сделать это соответственно, сравнивая с числами.

 .data
    prompt0: .asciiz "Please enter your string now (maximum of 40 characters):"
    prompt:.asciiz "\n Your current string is: "
    prompt1:.asciiz "\n Do you want to make any changes to the string? (Y/N): " 
    prompt2: .asciiz "\n Enter the character in the string would you like replaced:"
    prompt3: .asciiz "\n Enter what you would like to change the character to: "
    prompt4: .asciiz "\n Your current string is:"
     Buffer: .space 40
    .text
     li $v0, 4
     la $a0, prompt0
     syscall 

     li $v0,8        # take in input
     la $a0, Buffer      # load byte space into address
     li $a1, 40          # allot the byte space for string
     move $t0,$a0        # save string to t0
     syscall

    li $v0,4
    la $a0,prompt        # load and print "you wrote" string
    syscall

    la $a0, Buffer       # reload byte space to primary address
    move $a0,$t0         # primary address = t0 address (load pointer)
    li $v0,4         # print string
    syscall

     li $v0, 4      # ask user if they want to replace letter of not
     la $a0, prompt1
     syscall 


     li $v0,8        # take in input
     la $a0, Buffer      # load byte space into address
     li $a1, 1       # allot the byte space for string
     move $t0,$a0        # save string to t0
     bne $t0,$a0,replace
     beq $t0,$a0,exit
     syscall

    replace:
    li $v0,4
    la $a2,prompt2
    syscall

    li $v0,8
    la $a0, Buffer       # load byte space into address
    li $a1, 1        # allot the byte space for string
    move $t0,$a0 
    syscall

    exit:
    li $v0,10       #end program
    syscall     

i was supposed to get :

Your current string is:
We can input a fairly long string.
Do you want to make any changes to the string? (Y/N): Y
Enter the character in the string would you like replaced: i
Enter what you would like to change the character to: 1
Your current string is:
We can 1nput a fa1rly long str1ng.
Do you want to make any changes to the string? (Y/N): Y
Enter the character in the string would you like replaced: a
Enter what you would like to change the character to: @
Your current string is:
We c@n 1nput @ f@1rly long str1ng.
Do you want to make any changes to the string? (Y/N): N
Your final string is:
We c@n 1nput @ f@1rly long str1ng.
-- program is finished running –
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...