// read a line from stdin and and then an integer n
// Print the character in the nth-position
#include
// line of input stored here
char line[256];
int main(void) {
printf(“Enter a line of input: “);
fgets(line, 256, stdin);
printf(“Enter a position: “);
int n;
scanf(“%d”, &n);
printf(“Character is: “);
printf(“%c”, line[n]);
printf(“%c”, ‘\n’);
return 0;
}
// read a line and print its length
#include
// line of input stored here
char line[256];
int main(void) {
printf(“Enter a line of input: “);
fgets(line, 256, stdin);
int i = 0;
while (line[i] != 0) {
i++;
}
printf(“Line length: “);
printf(“%d”, i);
printf(“%c”, ‘\n’);
return 0;
}
// read a line and print whether it is a palindrom
#include
// line of input stored here
char line[256];
int main(void) {
printf(“Enter a line of input: “);
fgets(line, 256, stdin);
int i = 0;
while (line[i] != 0) {
i++;
}
int j = 0;
int k = i – 2;
while (j < k) {
if (line[j] != line[k]) {
printf("not palindrome\n");
return 0;
}
j++;
k--;
}
printf("palindrome\n");
return 0;
}
# read a line from stdin and and then an integer n
# Print the character in the nth-position
# n in $t0
main:
la $a0, str0 # printf("Enter a line of input: ");
li $v0, 4
syscall
la $a0, line # fgets(buffer, 256, stdin)
move $a3, $a0
la $a1, 256
li $v0, 8
syscall
la $a0, str1 # printf("Enter a position: ");
li $v0, 4
syscall
li $v0, 5 # scanf("%d");
syscall
move $t0, $v0
la $a0, str2 # printf("Character is: ");
li $v0, 4
syscall
add $t1, $a3, $t0
lw $a0, ($t1)
li $v0, 11
syscall
li $a0, '\n' # printf("%c", '\n');
li $v0, 11
syscall
li $v0, 0 # return 0
jr $ra
.data
str0:
.asciiz "Enter a line of input: "
str1:
.asciiz "Enter a position: "
str2:
.asciiz "Character is: "
# line of input stored here
line:
.space 256
# read a line and print its length
main:
la $a0, str0 # printf("Enter a line of input: ");
li $v0, 4
syscall
la $a0, line
la $a1, 256
li $v0, 8 # fgets(buffer, 256, stdin)
syscall #
la $a0, str1 # printf("Line length: ");
li $v0, 4
syscall
li $a0, 42 # printf("%d", i);
li $v0, 1
syscall
li $a0, '\n' # printf("%c", '\n');
li $v0, 11
syscall
li $v0, 0 # return 0
jr $ra
.data
str0:
.asciiz "Enter a line of input: "
str1:
.asciiz "Line length: "
# line of input stored here
line:
.space 256
# read a line and print whether it is a palindrom
main:
la $a0, str0 # printf("Enter a line of input: ");
li $v0, 4
syscall
la $a0, line
la $a1, 256
li $v0, 8 # fgets(buffer, 256, stdin)
syscall #
la $a0, not_palindrome
li $v0, 4
syscall
la $a0, palindrome
li $v0, 4
syscall
li $v0, 0 # return 0
jr $ra
.data
str0:
.asciiz "Enter a line of input: "
palindrome:
.asciiz "palindrome\n"
not_palindrome:
.asciiz "not palindrome\n"
# line of input stored here
line:
.space 256