简体   繁体   English

如何使用调整后的字符串索引使字符串第二次通过循环运行?

[英]How to get the string to be run through the loop a second time with the adjusted string index?

Trying to write code that will take an input string and "abbreviate" it. 尝试编写将接受输入字符串并“缩写”它的代码。 for example if aabb is given then the output should be bc. 例如,如果给定aabb,则输出应为bc。 Basically if there's any double letters it goes up to the next letter in the alphabet so aa = b and so on. 基本上,如果有任何双字母,它将升至字母表中的下一个字母,因此aa = b,依此类推。 This works fine for the first run through but it won't loop through until completion. 对于第一次运行,此方法工作正常,但直到完成,它才循环运行。 if the given string is aaaa the output should be c but instead the code outputs bb. 如果给定的字符串为aaaa,则输出应为c,但代码输出bb。 how can I adjust my code to run the string an appropriate amount of times? 如何调整代码以在适当的时间运行字符串?

string abbreviate (string loc){

int loopCount;
sort(loc.begin(), loc.end());

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


    if(loc[i] == loc[i+1]) {
        if(loc[i] == 'a'){
            loc[i] = 'b';
            loc[i+1] = 0;

        }else if(loc[i] == 'b'){
            loc[i] ='c';
            loc[i+1]= 0;
        }else if(loc[i] == 'c'){
            loc[i] ='d';
            loc[i+1]= 0;
        }else if(loc[i] == 'd'){
            loc[i] = 'e';
            loc[i+1]= 0;
        }else if(loc[i] == 'e'){
            loc[i] ='f';
            loc[i+1]= 0;
        }else if(loc[i] == 'f'){
            loc[i] ='g';
            loc[i+1]= 0;
        }else if(loc[i] == 'g'){
            loc[i] ='h';
            loc[i+1]= 0;
        }else if(loc[i] == 'h'){
            loc[i] ='i';
            loc[i+1]= 0;
        }else if(loc[i] == 'i'){
            loc[i] ='j';
            loc[i+1]= 0;
        }else if(loc[i] == 'j'){
            loc[i] ='k';
            loc[i+1]= 0;
        }else if(loc[i] == 'k'){
            loc[i] ='l';
            loc[i+1]= 0;
        }else if(loc[i] == 'l'){
            loc[i] ='m';
            loc[i+1]= 0;
        }else if(loc[i] == 'm'){
            loc[i] ='n';
            loc[i+1]= 0;
        }else if(loc[i] == 'n'){
            loc[i] ='o';
            loc[i+1]= 0;
        }else if(loc[i] == 'o'){
            loc[i] ='p';
            loc[i+1]= 0;
        }else if(loc[i] == 'p'){
            loc[i] ='q';
            loc[i+1]= 0;
        }else if(loc[i] == 'q'){
            loc[i] ='r';
            loc[i+1]= 0;
        }else if(loc[i] == 'r'){
            loc[i] ='s';
            loc[i+1]= 0;
        }else if(loc[i] == 's'){
            loc[i] ='t';
            loc[i+1]= 0;
        }else if(loc[i] == 't'){
            loc[i] ='u';
            loc[i+1]= 0;
        }else if(loc[i] == 'u'){
            loc[i] ='v';
            loc[i+1]= 0;
        }else if(loc[i] == 'w'){
            loc[i] ='x';
            loc[i+1]= 0;
        }else if(loc[i] == 'x'){
            loc[i] ='y';
            loc[i+1]= 0;
        }else if(loc[i] == 'y'){
            loc[i] ='z';
            loc[i+1]= 0;
        }


    }
}

return loc;
}

You can have a nested loop with a variable checking if something has been changed in the last loop iteration. 您可以使用带有变量的嵌套循环,以检查上次循环迭代中是否已更改某些内容。 For example, your code would look similar to this: 例如,您的代码将类似于以下内容:

// Pseudocode
bool changed = true; // This will keep track if previous loop has changed any values
while (changed) {
    changed = false; // Set to false at beginning of loop
    for (size_t i = 0; i < stringLength; ++i) {
        if (/*two same characters in a row*/) {
            // Change character to the next one in the alphabet
            changed = true; // This will indicate the outer loop to run again
        }
    }
}

This way, the program will only stop if all of the concurring characters have been changed. 这样,仅当所有并发字符都已更改时,程序才会停止。

There are a few other issues in your code such as indexing errors, here is an example how I implemented your functionality: https://wandbox.org/permlink/n0hk8SOpNEo6iZV7 您的代码中还有其他一些问题,例如索引错误,这是我如何实现您的功能的示例: https : //wandbox.org/permlink/n0hk8SOpNEo6iZV7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM