我正在尝试计算一个字符出现的次数,包括大写和小写,以及键盘输入字符串中的空格字符。
据我所知,在ASCII中,a-z为97 - 122,A-Z为65-90。我不知道如何在汇编中实现这个算法,因为我对它很新,这个概念让我很困惑。
我的伪代码是:
// Uppercase
array[0] = -65
if (character read from string > 64) {
if (character read from string < 91) {
increment array[i] based on character
}
}
// Print out integers stored in array (the frequency)
我不确定这是否是解决问题的方法。
.data
prompt: .asciiz "Please enter a string: \n"
userString: .space 104
occLow: .space 104
occUpp: .space 104
newLine: .asciiz "\n"
#######################
# t0 = string
# t1 = char being looked at
# t2 = counter
# s0 = array lower (occLow)
# s1 = array upper (occUpp)
#######################
.text
main: li $v0, 4
la $a0, prompt # Prompts the user for string
syscall
li $v0, 8 # Save string to $a0
la $a0, userString
move $t0, $a0 # Move $a0 to $t0
syscall
test: lb $t1, 0($t0) # Load each individual character
beqz $t1, end # If it is null, end program
addi $t0, $t0, 1 # Next char
beq $t1, 32, space # If the character is a space
blt $t1, 91, upper # Uppercase character, 90 = 'Z'
bgt $t1, 96, lower # Lowercase character
b test
upper1: bgt $t1, 64, upper2 # 65 = 'A'
b test
upper2: # UPPERCASE ADDITIONS HERE
# la $s0, occUpp
# add $t0, $t0, 4
# lw $t1, 0($s0)
# addi $t2, 1
# sw $t1,
答案 0 :(得分:0)
// Uppercase array[0] = -65 if (character read from string > 64) { if (character read from string < 91) { increment array[i] based on character } } // Print out integers stored in array (the frequency)
一些讨厌的风格笔记:
为什么&gt; 64和&lt;你写大写的时候是65到90?实际上大多数汇编程序都可以处理ASCII表单,因此大写从'A'
到'Z'
。
在伪代码中,我会尽力遵守我的初衷,因此请检查字符是否从65
到90
(包括)。
character = next_char();
if (character < 'A') goto not_uppercase;
if ('Z' < character) goto not_uppercase;
// uppercase detected
...
not_uppercase:
// character is not uppercase
...
“基于字符”
的增量数组[i]所以你希望i
基于角色。什么是性格?有些值从0到255,因为输入是ASCII编码的,所以单个字符是8位值。值0-255是否足以用于索引数组?实际上,如果您保留256个元素数组。通过检查不需要统计数据的代码,可以提高内存效率,例如只计算值32-127的统计数据,那么只需要96个计数器的数组,索引将是(character-32)
。如果您想将小写字母与大写字母一起计算,或者甚至将小写字母大写为-32
('a'-32 == 'A'
&lt; =&gt; 97-32 == 65
)。
但是您也可以使用256个元素数组来单独计算它们(甚至是不可打印的字符串内容),然后根据需要选择/分组大小写/小写/空白/数字部分。
那么伪代码可能如下所示:
charCounters = array(256) { 0, 0, ... };
// ^^^ counters for each possible letter set to zero
for_each(character in string) {
++charCounters[character]; // count particular letter
}
// counters are now updated, displaying results
// for example total count of uppercase
uppercaseLettersTotalCount = 0;
for (i = 'A' to 'Z') {
uppercaseLettersTotalCount += charCounters[i];
}
display("Total uppercase letters: " + uppercaseLettersTotalCount);
// number of 'A' letters, both upper and lowercase
totalA = charCounters['A'];
totalA += charCounters['a'];
display("Total A/a letters: " + totalA);
// ... etc
如果你只想显示一个短字符串(5-30个字母)中单个字符的计数,那么做一个天真的破坏性的两个内部循环,通过字符串char by char并计算字符串剩余部分中的相同字符:
for (string_index in [0, string_length - 1]) {
character = string[string_index];
if (0 == character) continue; // was already counted
count = 1;
for (index2 in [string_index + 1, string_length - 1]) {
if (character != string[index2]) continue; // different char
++count; // same character found
string[index2] = 0; // mark as counted (destroys input string)
}
display("Character " + character + " found " + count + " many times.\n");
}