String MIPS,位于堆栈中的字符串中出现的次数

String MIPS,位于堆栈中的字符串中出现的次数,string,stack,mips,String,Stack,Mips,我有一个练习要在MIPS汇编中解决(我有一些疑问,但其他事情很清楚),但我在编写代码时遇到了一些问题。这个练习问我: 编写一个程序,从键盘上获得一个字符串,对出现次数较多的字符的出现次数进行计数并显示 我如何检查所有26个字符,并找出谁的出现率较高 例如: 给我一根线:你好,世界! 出现次数较高的字符为:l 非常感谢你以后的回答 附言。 这是我计划的第一部分: #First message li $v0, 4 la $a0, mess

我有一个练习要在MIPS汇编中解决(我有一些疑问,但其他事情很清楚),但我在编写代码时遇到了一些问题。这个练习问我: 编写一个程序,从键盘上获得一个字符串,对出现次数较多的字符的出现次数进行计数并显示

我如何检查所有26个字符,并找出谁的出现率较高

例如: 给我一根线:你好,世界! 出现次数较高的字符为:l

非常感谢你以后的回答

附言。 这是我计划的第一部分:

        #First message      
        li $v0, 4
        la $a0, mess
        syscall

        #Stack space allocated
        addi $sp, $sp, -257 

        #Read the string
        move $a0, $sp
        li $a1, 257
        li $v0, 8
        syscall

由于这是您的任务,我将把MIPS程序集的实现留给您。我将用更高级的语言向您展示代码的逻辑:

#include <stdio.h>

int main()
{
    //Maximum length of string:
    int stringMaxLength = 100;

    //Create string in stack. Size of string is length+1 to
    //allow the '\0' character to mark the end of the string.
    char str[stringMaxLength + 1];

    //Read a string of maximum stringMaxLength characters:
    puts("Enter string:");
    scanf("%*s", stringMaxLength, str);
    fflush(stdin);

    //Create array of counters in stack:
    int counter[26];

    //Initialize the counters to 0:
    int i;
    for (i=0; i<26; ++i)
        counter[i] = 0;

    //Main counting loop:
    for (i=0; str[i] != '\0'; ++i)
    {
        char tmp = str[i]; //Storing of str[i] in tmp, to write tmp if needed,
        //instead of writing str[i] itself. Optional operation in this particular case.

        if (tmp >= 'A' && tmp <= 'Z') //If the current character is upper:
            tmp = tmp + 32; //Convert the character to lower.

        if (tmp >= 'a' && tmp <='z') //If the character is a lower letter:
        {
            //Obtain the index of the letter in the array:
            int index = tmp - 'a';

            //Increment its counter by 1:
            counter[index] = counter[index] + 1;
        }
        //Else if the chacacter is not a lower letter by now, we ignore it,
        //or we could inform the user, for example, or we could ignore the
        //whole string itself as invalid..
    }

    //Now find the maximum occurences of a letter:
    int indexOfMaxCount = 0;
    int maxCount = counter[0];
    for (i=1; i<26; ++i)
        if (counter[i] > maxCount)
        {
            maxCount = counter[i];
            indexOfMaxCount = i;
        }

    //Convert the indexOfMaxCount back to the character it corresponds to:
    char maxChar = 'a' + indexOfMaxCount;

    //Inform the user of the letter with maximum occurences:
    printf("Maximum %d occurences for letter '%c'.\n", maxCount, maxChar);

    return 0;
}
//您可以将这些变量保存在您选择的一些MIPS寄存器中
int c,i,count,max_count=0;
char max_char;
//迭代所有ASCII字符代码
对于(c=0;c<128;c+=1){
计数=0;
//计算此字符在字符串中出现的次数
对于(i=0;字符串[i]!=0;i+=1){
if(string[i]==c)count++;
}
//是否大于当前最大值?
如果(计数>最大计数){
最大计数=计数;
max_char=c;
}
}
//max_char现在保存数字最大的字符的ASCII码
//和max_count保留该字符被删除的次数
//在字符串中找到。

@Michael,我在发帖前看到你的回答,我只想用更详细的回答重复一遍。如果你编辑你自己的,添加一些更多的解释,那么我会删除我的。我没有直接编辑你的,因为你发帖的时候我已经到了一半了。无论如何:

@马可:
您可以创建26个计数器的临时数组(初始化为0)

每个计数器对应每个字母(即每个字母出现的数字)。例如,
计数器[0]
对应字母“a”的出现次数,
计数器[1]
对应字母“b”等

然后迭代输入字符序列中的每个字符,并对每个字符执行:
a) 获取
计数器中字符的索引。
b) 将
计数器[“获取的索引”]
增加1

要获取字符的索引,可以执行以下操作:
a) 首先确保字符不是大写,即只允许“a”到“z”,而不允许“a”到“z”。如果不是,则将其转换。
b) 从字符中减去字母“a”。这样,“a”-“a”给0,“b”-“a”给1,“c”-“a”给2,等等

我将用C语言演示,因为这是关于MIPS的练习(我的意思是目标是学习MIPS汇编语言):


非常感谢,非常有用的回答。明天我试着编码,因为我有一些疑问。。。该练习不区分大小写,因此更易于编码。总结:-我创建了一个26个整数的数组(每个字母的计数器)-迭代字符串的每个字母-明天更新数组中的相对索引我尝试更新您的新问题。。。多谢各位much@Marco好的:)。忘了显式写入以查找主循环后的最大出现次数,然后将索引转换回字符,但我将其包含在代码示例中,我认为这应该是显而易见的。我每天都在登录(以获取徽章:P),因此我将在下面跟踪您的评论。另外,欢迎来到StackOverflow:)谢谢你的欢迎:)我关注这个网站很长时间了,但是我在没有注册的情况下找到了我的解决方案XD我开始编码,祝我好运哈哈哈对不起我的英语不好。。。
printf("%c", 'a'); //Prints 'a'.  
printf("%d", (int) 'a'); //Prints '97'.  
printf("%c", (char) 97); //Prints 'a'.  
printf("%d", 97); //Prints '97'.  
printf("%d", (int) 'b'); //Prints '98'.  
printf("%c", (char) (97 + 1)); //Prints 'b'.  
printf("%c", (char) ( ((int) 'a') + 1 ) ); //Prints 'b'.  
//Etc...  
//All the casting in the above examples is just for demonstration,  
//it would work without them also, in this case.