caesar:从z循环的小写字母不打印

时间:2016-12-16 03:44:53

标签: c cs50 caesar-cipher

我有点坚持这个。当我运行我的程序时,所有循环过z的字母都不会出于某种原因打印。问题来自这个链接:http://docs.cs50.net/2016/x/ap/problems/caesar/caesar.html这是我的代码:

function disable(idCheckbox) {
  var checkbox = document.getElementById(idCheckbox);
  checkbox.setAttribute("disabled", "false");
}

当我输入" ./ caesar 13" (13是加密密钥)进入我的终端,我被提示输入我想要的文本。但是,当我输入" matteo maTTeo"时,我得到输出:" znrb znGGrb"。为什么会这样?我对编码很陌生,所以如果答案很明显,那就请放心吧!谢谢!

2 个答案:

答案 0 :(得分:0)

                   ...
                    if (isupper(text[i + 1]))
                    {
                        text[i + 1] = text[i + 1] + k;
                        if (text[i + 1] > 'Z')
                        {
                            text[i + 1] = text[i + 1] - 26;
                            printf("%c", text[i + 1]);
                        }
                        else
                        printf("%c", text[i + 1]);
                    }
                    else if (islower(text[i + 1]))
                    {
                        text[i + 1] = text[i + 1] + k;
                        if (text[i + 1] > 'z')
                        {
                            text[i + 1] = text[i + 1] - 26;
                            printf("%c", text[i + 1]);
                        }
                        else
                        printf("%c", text[i + 1]);
                    }
                    i++;
                    ......

在检查空间的情况下不需要此代码。它将在下一次迭代中自动处理。

答案 1 :(得分:0)

text[i] > 'z'text[i]可能是负数。                     –́BLUEPIXY

修正:if ((unsigned char)text[i] > 'z')