使用 strdup()是一种方法,但是如果您可以修改 text 本身,则有一个更好的选择。您可以简单地使用:
void encrypt(char *text, int k) {
while (*text) {
// Identify if the value is an alphabet
if (isalpha(*text) != 0) {
if (isupper(*text) != 0) {
// Apply formula to uppercase character
*text = ((*text - 'A' + k) % 26) + 'A';
} else {
// Apply formula to lowercase character
*text = ((*text - 'a' + k) % 26) + 'a';
}
}
text++;
}
}
一些解释: while(*text)表示以下内容,直到存储 at text是0。 text ++; 增加指针。因此,在该行之后,文本指向下一个字符。所有的操作只是(int i = 0; text [i]!='\ 0'; i ++)的另一种做法,但不需要新变量。
As mentioned in the comments, you are writing to a NULL pointer since you set ciphertext to NULL. What you want to do is allocate memory for ciphertext. This can be done with char *ciphertext = strdup(text); which sets up ciphertext with a newly allocated pointer to a copy of the null-terminated char pointer named text. Using either this solution or the one below, you don't need to // Store non-alphabetical value as it is since that value is already there.
Using strdup() is one method, but there is a better option if you can modify text itself. You can simply use:
void encrypt(char *text, int k) {
while (*text) {
// Identify if the value is an alphabet
if (isalpha(*text) != 0) {
if (isupper(*text) != 0) {
// Apply formula to uppercase character
*text = ((*text - 'A' + k) % 26) + 'A';
} else {
// Apply formula to lowercase character
*text = ((*text - 'a' + k) % 26) + 'a';
}
}
text++;
}
}
A little explanation: while (*text) means do the following until the value stored at text is 0. text++; increments the pointer. So after that line text is pointing to the next char. All that does is just another way of doing for (int i = 0; text[i] != '\0'; i++) but without needing a new variable.
Note that the above code no longer returns the result of the "encryption". Rather, it modifies the content of the char * you passed it. No memory allocation necessary-- the caller did it for you.
If you opt to use the above code, you'll need to do something like this to print the result:
发布评论
评论(1)
如注释中所述,您正在将其写入零指针,因为您将
ciphertext
设置为null。您要做的是为ciphertext
分配内存。这可以使用char *ciphertext = strdup(text);
设置ciphertext
,并带有新分配的指针,可用于null终止的char指数文本
。使用此解决方案或下面的解决方案,您不需要//存储非按字母的值,因为它已经存在
,因为该值已经存在。使用
strdup()
是一种方法,但是如果您可以修改text
本身,则有一个更好的选择。您可以简单地使用:一些解释:
while(*text)
表示以下内容,直到存储 at text是0。text ++;
增加指针。因此,在该行之后,文本
指向下一个字符。所有的操作只是(int i = 0; text [i]!='\ 0'; i ++)的另一种做法,但不需要新变量。请注意,以上代码不再返回“加密”的结果。相反,它修改了您传递的
char *
的内容。无需内存分配 - 呼叫者为您做到了。如果您选择使用上述代码,则需要执行类似的操作来打印结果:
As mentioned in the comments, you are writing to a NULL pointer since you set
ciphertext
to NULL. What you want to do is allocate memory forciphertext
. This can be done withchar *ciphertext = strdup(text);
which sets upciphertext
with a newly allocated pointer to a copy of the null-terminated char pointer namedtext
. Using either this solution or the one below, you don't need to// Store non-alphabetical value as it is
since that value is already there.Using
strdup()
is one method, but there is a better option if you can modifytext
itself. You can simply use:A little explanation:
while (*text)
means do the following until the value stored at text is 0.text++;
increments the pointer. So after that linetext
is pointing to the next char. All that does is just another way of doingfor (int i = 0; text[i] != '\0'; i++)
but without needing a new variable.Note that the above code no longer returns the result of the "encryption". Rather, it modifies the content of the
char *
you passed it. No memory allocation necessary-- the caller did it for you.If you opt to use the above code, you'll need to do something like this to print the result: