gpt4 book ai didi

将字符转换为位于 argv[1] 内字符串内的 int

转载 作者:行者123 更新时间:2023-11-30 17:12:22 25 4
gpt4 key购买 nike

我正在尝试在存储在 argv[1] 中的字符串和名为 k 的 int 变量中转换字符,该变量将具有其相应的 ASCII 值。

此外,我正在转换的这个字符也将通过另一个名为 c 的 int 变量控制的 for 循环不断变化。我在下面发布代码。

让我知道我做错了什么,因为我不断收到许多不同的错误消息。

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

int main(int argc, string argv[])
{
int a = argc;
if (a != 2)
{
return 1;
}

string b = argv[1];
//b IS THE CODE WORD
string ptext;
//ptext IS THE MESSAGE TO BE ENCRYPTED

if (isalpha(b))
{
ptext = GetString();
}
else
return 1;

//this is to index the letters within the code word
for (int c = 0, d = strlen(b); c < d; c++)
{
int k = (b(char[c]));
//this is to index the letters within the plaintext
for (int i = 0, l = strlen(ptext); i < l ;i++)
{
while (isalpha(ptext[i]))
{
printf("%c", b[c]%d+k);
}
}
}
}

有问题的部分距离底部 11 行。

最佳答案

我尝试根据我的 Linux 系统修改您的代码,并根据您提供的数据(或者更确切地说没有提供的数据)在代码中进行少量修改,然后在此处发布。

我希望这可以帮助您了解代码中的错误。

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

#define string char*
int main(int argc, string argv[])
{
int a = argc;
if (a != 2)
{
return 1;
}

string b = argv[1];
//b IS THE CODE WORD
string ptext;
//ptext IS THE MESSAGE TO BE ENCRYPTED

if (isalpha(b))
{
//ptext = GetString();
ptext = "Hello World";
}
else
return 1;

//this is to index the letters within the code word
int c = 0;
int d;
for (c = 0, d = strlen(b); c < d; c++)
{
//int k = (b(char[c]));
int k = *(b+c);
//this is to index the letters within the plaintext
int i = 0;
int l;
for (i = 0, l = strlen(ptext); i < l ;i++)
{
//This will cause buffer overflow, so commenting
/*
while (isalpha(ptext[i]))
{
printf("%c", b[c]%d+k); //what are you trying to print as %d here
}
*/
}
}
}

关于将字符转换为位于 argv[1] 内字符串内的 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31601358/

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