我正在编写一个程序,使用凯撒的算法来加密一个字符串输入。我是C的初学者,但我能理解基本的代码。因此,要对我编写的代码进行加密,但是当我输入输入时,我会得到一个错误,即
分割故障(弃核)
我试着进行一些调试,方法是移除末尾的其他条件,程序类型可以用于2-3个字母的短输入。
有人能帮我解决这个问题吗?
首先,我使用in 50的头来获取字符串。
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
char name[] = "";
strcat(name, argv[1]);
int key = atoi(name);
string plaintext = get_string("plaintext: ");
int length = strlen(plaintext);
char ciphertext[] = "";
for(int i = 0; i < length; i++)
{
int skipCount = 0;
if(isalpha(plaintext[i]))
{
while(skipCount < key)
{
char tmp = (char) ((int) plaintext[i] + 1);
if(isalpha(tmp))
{
ciphertext[i] = tmp;
skipCount++;
}
else
{
if (isupper(plaintext[i]))
{
tmp = 'A';
skipCount++;
}
if (islower(plaintext[i]))
{
tmp = 'a';
skipCount++;
}
}
}
}
else ciphertext[i] = plaintext[i];
}
printf("%s\n", ciphertext);
}发布于 2022-04-17 16:40:11
关于C,您需要了解的是它不会自动分配内存。
你必须做好你自己!
这一行:
char name[] = "";创建一个大小为1的数组,该数组包含一个字符-- "null“字符= '\0';
它表示空字符串。
您不能将任何较大的字符串复制到它,因为C中的所有字符串都必须在末尾有一个空字符,因此即使是一个可读的字符也没有足够的空间。
作为初学者,您需要确定您想要的字符串的最大长度,并声明数组的适当大小:
char name[255];这是一个可以容纳254个字符的示例,再加上终止空字符。
https://stackoverflow.com/questions/71903568
复制相似问题