ASCII出現頻率

 avatar
user_6817964
c_cpp
a year ago
940 B
3
Indexable
Never
for (int i = 0; i < 128; i++) {
            myData[i].frequency = 0;
        }
        len = strlen(str);

        for (int i = 0; i < len; i++) {
            myData[str[i]].charASCII = str[i];
            myData[str[i]].frequency++;
        }

        for (int i = 127; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (myData[j].frequency > myData[j + 1].frequency) {
                    struct pair temp = myData[j];
                    myData[j] = myData[j + 1];
                    myData[j + 1] = temp;
                }
                else if (myData[j].frequency == myData[j + 1].frequency){
                    if (myData[j].charASCII < myData[j + 1].charASCII) {
                        struct pair temp = myData[j];
                        myData[j] = myData[j + 1];
                        myData[j + 1] = temp;
                    }
                }
            }
        }