=CS50 PSET 2 CAESAR= 如何将密钥转换为数字?如果我使用 atoi() ,它会给我这个错误
我在 only_digits
函数中遇到 atoi()
问题。我在 Discord 上询问,他们说我将 char 类型 arg 传递给 atoi() ,这不起作用,因为 atoi() 只接受 string 或 char * (array char) 作为参数。我不明白。我对字符串和字符的区别感到困惑。我不是将 argv[1]
(这是一个字符串)传递给 only_digits
吗?这意味着 inputKey
也是一个字符串?那么我将 char 类型 arg 传递给 atoi() 意味着什么?我究竟如何使 atoi()
工作?我被这个问题困扰了两天了。
// Encrypts text using Caesar's cipher
// ci = (pi + k) % 26
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
bool only_digits (string inputKey);
char rotate (char plaintext[], int key);
int main(int argc, string argv[])
{
string p;
// Make sure program was run with just one command-line argument
if (argc != 2)
{
printf("Enter exactly one input\n");
return 1;
}
// Make sure every character in argv[1] is a digit (DOESN'T WORK)
/*else if (only_digits(argv[1]))
{
printf("Usage: ./caesar key\n");
return 1;
}*/
else
{
p = get_string("plaintext: ");
}
// Convert argv[1] from a string to an int
int k = atoi(argv[1]);
char c[strlen(p) + 1];
// Convert ASCII range down to a value from 0 to 25
// For each character in the plaintext: (DOESN'T WORK)
for (int i = 0, n = strlen(p); i <= n; i++)
{
// Rotate the character if it's a letter // ci = (pi + k) % 26
if (isalpha(p[i]))
{
if (isupper(p[i]))
{
c[i] = ((p[i]) + k) % 26;
}
else if (islower(p[i]))
{
c[i] = ((p[i]) + k) % 26;
}
}
}
printf("ciphertext: %s\n", c);
}
// Function to encrypt plaintext
/*char rotate (char plaintext[], int key)
{
char c[strlen(plaintext) + 1];
return c;
}*/
// Function to check if key is a digit (DOESN'T WORK)
bool only_digits (string inputKey)
{
int flag = 0;
for (int i = 0, n = strlen(inputKey); i < n; i++)
{
// Converts string input to int
int response = atoi(inputKey[i]);
// Check if it is a digit
if (!(isdigit(response)))
{
flag++;
}
}
if (flag != 0)
{
return false;
}
else
{
return true;
}
}
I'm having a problem with atoi()
in the only_digits
function. I asked on discord and they said that I am passing a char type arg to atoi()
which doesn't work since atoi()
only takes string or char * (array of char) as arguments. I don't get it. I'm confused with the difference of string and char. Aren't I passing argv[1]
(which is a string) to only_digits
? Which means inputKey
is a string as well? So what do they mean that I am passing a char type arg to atoi()
? How exactly do I make atoi()
work? I'm stuck with this problem for 2 days now.
// Encrypts text using Caesar's cipher
// ci = (pi + k) % 26
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
bool only_digits (string inputKey);
char rotate (char plaintext[], int key);
int main(int argc, string argv[])
{
string p;
// Make sure program was run with just one command-line argument
if (argc != 2)
{
printf("Enter exactly one input\n");
return 1;
}
// Make sure every character in argv[1] is a digit (DOESN'T WORK)
/*else if (only_digits(argv[1]))
{
printf("Usage: ./caesar key\n");
return 1;
}*/
else
{
p = get_string("plaintext: ");
}
// Convert argv[1] from a string to an int
int k = atoi(argv[1]);
char c[strlen(p) + 1];
// Convert ASCII range down to a value from 0 to 25
// For each character in the plaintext: (DOESN'T WORK)
for (int i = 0, n = strlen(p); i <= n; i++)
{
// Rotate the character if it's a letter // ci = (pi + k) % 26
if (isalpha(p[i]))
{
if (isupper(p[i]))
{
c[i] = ((p[i]) + k) % 26;
}
else if (islower(p[i]))
{
c[i] = ((p[i]) + k) % 26;
}
}
}
printf("ciphertext: %s\n", c);
}
// Function to encrypt plaintext
/*char rotate (char plaintext[], int key)
{
char c[strlen(plaintext) + 1];
return c;
}*/
// Function to check if key is a digit (DOESN'T WORK)
bool only_digits (string inputKey)
{
int flag = 0;
for (int i = 0, n = strlen(inputKey); i < n; i++)
{
// Converts string input to int
int response = atoi(inputKey[i]);
// Check if it is a digit
if (!(isdigit(response)))
{
flag++;
}
}
if (flag != 0)
{
return false;
}
else
{
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C 中的字符串被定义为非零字节(字符)序列,后跟一个空终止字节:
atoi
(ASCII 到整数)需要正确的以 null 结尾的字符串 (<代码>字符*)。您不能向其传递单个char
。isdigit
需要一个 < em>角色。在您的程序中,
inputKey[i]
是一个字符。您可以直接使用isdigit
进行测试,无需事先将其转换为整数表示。如果遇到非数字字符,您也可以提前返回。
注意:
size_t
是strlen
的返回类型,也是最适合索引内存的类型。Strings in C are defined as a sequence of nonzero bytes (characters), followed by a null-terminating byte:
atoi
(ASCII to integer) expects a proper null-terminated string (char *
). You can not pass it a singlechar
.isdigit
expects a single character.In your program
inputKey[i]
is a single character. You can test it directly withisdigit
, there is no need to convert it to an integer representation beforehand.You can also simply return early if you encounter a non-digit character.
Note:
size_t
is the return type ofstrlen
, and the most appropriate type for indexing memory.