gpt4 book ai didi

凯撒 : lowercase letters that loop from z are not printing

转载 作者:行者123 更新时间:2023-11-30 19:36:17 27 4
gpt4 key购买 nike

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

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>

int main(int argc, string argv[])
{
if (argv[1] != NULL)
{
int k = atoi(argv[1]);
if ((argc == 2) && (k >= 0))
{
printf("Type what you would like to encrypt!\n");
string text = GetString();
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isalpha(text[i]) || isspace(text[i]))
{
if (isupper(text[i]))
{
text[i] = text[i] + k;
if (text[i] > 'Z')
{
text[i] = text[i] - 26;
printf("%c", text[i]);
}
else
printf("%c", text[i]);
}
else if (islower(text[i]))
{
text[i] = text[i] + k;
if (text[i] > 'z')
{
text[i] = text[i] - 26;
printf("%c", text[i]);
}
else
printf("%c", text[i]);
}
else if (isspace(text[i]))
{
printf(" ");
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++;
}
}
else
printf("%c", text[i]);
}
printf("\n");
return 0;
}
else
printf("Usage: ./caesar <non-negative integer>\n");
return 1;
}
else
printf("Usage: ./caesar <non-negative integer>\n");
return 1;
}

当我在终端中输入“./caesar 13”(13 是加密 key )时,系统会提示我输入所需的文本。但是,当我输入“matteo maTTeo”时,我得到的输出是:“znrb znGGrb”。为什么会这样呢?我对编码还很陌生,所以如果答案很明显,请对我放轻松!谢谢!

最佳答案

                   ...
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++;
......

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

关于凯撒 : lowercase letters that loop from z are not printing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41176967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com