CS50 pset2 Vigenere cypher的代码如下。我是C编程的新手。
[我在得到一些建议后编辑了代码,这段代码(如下)是我新编辑的代码。]
当我运行代码时,它会产生无限循环,并且新的加密文本也不会按原样产生。我可以就纠正我的代码获得一些建议和建议吗? 谢谢,
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2) //if it is not rqual to 2, it gives an error message.
{
printf("Enter the valid input : \n");
return 1;
}
if (argc == 2) //if two commands are given then it proceeds to other step.
{
string k = argv[1];
string m = GetString();
int l = strlen(k);
int p = strlen(m);
for( int i = 0; i <= p ; i++ ) //it has to keep on rotating from 0 to len of string and back to zero and so on.
{
{
i = i % l;
}
if (isalpha(m[i]) && isalpha(k[i])) // it proceeds ahead only if the input given is an alphabet, if the input is sth other than alphabet it prints exactly as it is.
{
for(int t = 0; t <= p ; t++)
{
if(isupper(m[t])) // when is it capital letter.
{
printf("%c", ( m[t] - 65 + k[i]) % 26 + 65);
}
if(islower(m[t])) // when it is small letter.
{
printf("%c" , ( m[t] - 97 + k[i])% 26 + 97);
}
}
}
else //if it is not an alphabet it returns as it is.
{
printf("%c", m[i]);
}
}
}
printf("\n");
return 0;
}
答案 0 :(得分:0)
让我们来看看错误。它表示你在那里给出的参数不是数组,而是将它用作数组。这是正确的:p是一个整数,而不是一个数组:
int p = strlen(msg);
使用p [i]表示您要访问p数组的元素编号i。但是不可能达到这个值,因为p只是一个整数变量,而不是一个数组。
您可能想要用作数组的是您的字符串参数之一,键或消息。 CS50中的字符串变量相当于经典C中的char *变量,并用作字符数组。