Declare a string in the data section such as: .data string: .asciiz \"in a hole
ID: 3586817 • Letter: D
Question
Declare a string in the data section such as:
.data
string: .asciiz "in a hole in the ground there lived a hobbit"
Notice there are extra spaces in this string. Write an MIPS program that capitalizes the first letter of each word, so that after running your program the data will look like this:
.data
string: .asciiz "In A Hole In The Ground There Lived A Hobbit"
Assume that the data comprises only upper and lower case characters and spaces, and alters a character only if it is lower case, and follows a space or is the first character on the line.
Print the strings before and after translation by using the syscall print string service.
Here is my solution:
.text
.globl main
main:
li $v0, 4 # Print the original string.
la $a0, string
syscall
loop:
lb $t1, string($t0)
nop
nop
beq $t1, $0, end # if ($t1 == NULL) print the result
nop
nop
bne $t0, $0, next # Check if first character is lower case
nop
nop
blt $t1, 97, next2 # Convert from lower case to upper case
nop
nop
bgt $t1, 122, next2
nop
sub $t1, $t1, 32
sb $t1, string($t0)
addi $t0, $t0, 1
j loop
next:
bne $t1, 32, next2 # if ($t1 == ' ') check for next character
nop
addi $t0, $t0, 1
lb $t1, string($t0)
nop
nop
blt $t1, 97, next2 # Convert from lower case to upper case
nop
nop
bgt $t1, 122, next2
nop
sub $t1, $t1, 32
sb $t1, string($t0)
addi $t0, $t0, 1
j loop
next2:
addi $t0, $t0, 1
j loop
end:
li $v0, 4 # Print a new line.
la $a0, newline
syscall
li $v0, 4 # Print the new string.
la $a0, string
syscall
li $v0, 10 # End the program.
syscall
.data
string: .asciiz "in a hole in the ground there lived a hobbit"
newline: .asciiz " "
Here is my output:
"In A Hole In The Ground There Lived A hobbit"
I can't seem to capitalize the letter "h" in "hobbit". Can someone help me fix this?
Explanation / Answer
Find the code in java.
import java.util.*;
import java.lang.*;
import java.io.*;
class Capitalize {
public static String capitalize(String s) {
if (s.length() == 0)
return s;
return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
}
public static void main(String[] args) {
String line = "in a hole in the ground there lived a hobbit";
String[] words = line.split("\s");
for (String s : words) {
System.out.print(capitalize(s) + " ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.