MIPS。计算字符串中每个ascii符号的数量。

时间:2017-03-22 15:25:49

标签: string assembly ascii mips

我正在尝试完成MIPS中的任务。

需要以下一种方式比较2个字符串:在两个字符串中计算从1到255的每个ascii符号的数量,并比较每个符号的值。 有一会儿,我在java中写了一段伪代码,只是为了想象一个问题。我无法理解的部分是如何对字符串中的每个ascii字符进行“搜索”。

 // array allAscii that suppose to contain all ascii  symbols (i have made it
 containing all zeros bacause in this java code it is not that much important)
int[] allAscii = new int[255]               

String s1 = "ascii.";   // first string
String s2 = "asciiz,";   // second string    

int difference = 0;    

for(int i = 0; i< allAscii.length; i++){

    int count1 = 0;       //counter for the character of the string1
    int count2 = 0;       //counter for the character of the string1

    for(int k = 0; k<s1.length(); k++){
        if( s1.charAt(k) == allAscii[i]) count1++;              
    }           
    for(int l = 0; l<s2.length(); l++){
        if( s2.charAt(l) == allAscii[i]) count2++;
    }

    difference += Math.abs(count1- count2);         
}

所以字符串“ascii”的结果。和“asciiz”,

从0到43的符号:count1 = 0; count2 = 0;差异= 0;

',':count1 = 0; count2 = 1;差异= 1;

'。':count1 = 1; count2 = 0;差异= 1;

符号从47到96:count1 = 0; count2 = 0;差异= 0;

'a':count1 = 1; count2 = 1;差异= 0;

'c':count1 = 1; count2 = 1;差异= 0;

'i':count1 = 2; count2 = 2;差异= 0;

's':count1 = 1; count2 = 1;差异= 0;

'z':count1 = 0; count2 = 1;差异= 1;

依旧......

不需要存储每个字母的数量。我只需要积累变量差异,但我应该通过所有ascii符号

0 个答案:

没有答案