C语言 休息的效果是什么;在本代码中

wvt8vs2t  于 2023-03-07  发布在  其他
关注(0)|答案(2)|浏览(189)

我有一个用ROT13编码给定字符串的程序。但是,我意识到如果我不在if statement中的某个地方添加一个break;,那么编码就有部分错误。我想知道为什么。

#include <stdio.h>

int main() {
    char s1[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n";
    char s2[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n";
    char codec[] = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
    char ROT13[] = "nNoOpPqQrRsStTuUvVwWxXyYzZaAbBcCdDeEfFgGhHiIjJkKlLmM";
    int i =0;
    int j =0;

    printf("%s\n",s1);  //prints source string
    
    for (i = 0; s1[i] !='\0'; i++) {
        for (j = 0; codec[j] != '\0'; j++) {
            if (s1[i] == codec[j]) {
                s1[i] = ROT13[j];
                break;
            }
        }
    }
    printf("%s\n", s1);   //Output after ROT13 encoding
    
    for (i = 0; s2[i] !='\0'; i++) {
        for (j = 0; codec[j] != '\0'; j++) {
            if (s2[i] == codec[j]) {
                s2[i] = ROT13[j];
                //break;
            }
        }
    }
    printf("%s", s2);   //Output after ROT13 encoding

    return 0;

}

编译后的结果。

eoigrqb6

eoigrqb61#

break;使它退出循环。
例如,当明文是c时,它被加密为p,如果没有break;,则继续扫描,找到p,并再次将其加密为c
break;是为了防止这种双重加密。

ffscu2ro

ffscu2ro2#

在第二个字符匹配循环中需要break,原因与在第一个字符匹配循环中需要break相同。如果在codec中找到与输入字符串中的当前字符匹配的字符,则替换在输入字符串中找到的字符。s1s2。然后,您需要在替换字符后立即退出字符匹配循环,以前进到输入字符串中的下一个字符。
如果在替换输入字符串中的当前字符后没有前进到输入字符串中的下一个字符,则可以匹配替换字符并进行第二次旋转。是否进行第二次旋转取决于要匹配的字符。
示例:如果输入字符串中的字符是'A',然后替换为'N',并且您继续查找匹配项,您将找到'N',然后替换已经替换为'N'的原始字符'A',第二次使用'A'。这种双重替换将输入字符旋转13,然后再次旋转13,进行两次旋转。
对于第13个字符之后的字符('M''m'),您会很幸运,因为这些字符会旋转到字母表第一部分的字符,因此这些字符不会经历双重替换。
您也可以考虑使用strchr()标准C库函数替换执行字符匹配操作的最内层循环的替代实现。

#include <stdio.h>
#include <string.h>

int main() {
    // the codec contains characters arranged so that the replacement character
    // for a character in the codec is the position of the matching character
    // plus 13. We do this by having the characters in ascending order from
    // letter A to letter Z then duplicating the first 13 characters of the
    // alphabet after the letter Z. This allows us to find a match and then
    // just add 13 to the index to find the replacement character.
    // WARNING: This array of alphabetic characters is designed to be used
    //          with the strchr() function which find only the first matching
    //          character so duplicating the first 13 characters doesn't matter.
    char codec2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNabcdefghijklmnopqrstuvwxyzabcdefghijklmn";
    char s1[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n";
    char s2[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n";

    printf("%s\n", s1);  //prints source string

    for (int i = 0; s1[i] != '\0'; i++) {
        char* p;
        if (p = strchr(codec2, s1[i])) {
            // found a matching character so replace it.
            // codec2[] contains the replacement character 13 positions after
            // the matching character.
            s1[i] = *(p + 13);
        }
    }
    printf("%s\n", s1);   //Output after ROT13 encoding

    for (int i = 0; s2[i] != '\0'; i++) {
        char* p;
        if (p = strchr(codec2, s2[i])) {
            s2[i] = *(p + 13);
        }
    }
    printf("%s", s2);   //Output after ROT13 encoding

    return 0;
}

相关问题