gpt4 book ai didi

c - 输出中出现奇怪的字符

转载 作者:行者123 更新时间:2023-11-30 21:08:26 25 4
gpt4 key购买 nike

我试图混淆存储在字符串中的单词,我的代码有时有效,有时无效。这是我的代码:

// main function
int main(int argc, string argv[])
{
string k, plaintext;
int size, i = 0, key = 0;
k = argv[1];
size = strlen(k);
if (argc < 2 || !isNummeric(k, size) || k < 0)
return 1;
else
plaintext = GetString();
size = strlen(plaintext);
char ciphertext[size];
key = atoi(k);
while(i < size)
{
if (isalpha(plaintext[i]))
{
encipher(key, i, &ciphertext[i], plaintext);
}
else
{
ciphertext[i] = plaintext[i];
}
i++;
}
printf("%s\n",ciphertext);
}

从用户处收到一个用于移动每个字母的 key ,我需要检查该 key 是否是数值,因此我创建了 isNummeric 函数来执行此操作

bool isNummeric(string k, int size)
{
int c=0;
for(int i=0; i<size; i++)
{
if(!isdigit(k[i]))
c++;
}
if(c==0)
return true;
return false;
}

现在为了加密,我制作了移动每个字母的函数:

void encipher(int k, int i, char *pt, string plaintext)
{
int p, c;
if(islower(plaintext[i]))
{
p=plaintext[i]-'a';
c=(p+k)%26;
*pt=c+97;
}
else
{
p=plaintext[i]-'A';
c=(p+k)%26;
*pt=c+65;
}
}

here is my output

最佳答案

将问题中的多个评论组装成固定代码会产生以下似乎有效的代码:

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

static bool isNumeric(string k, int size)
{
for (int i = 0; i < size; i++)
{
if (!isdigit((unsigned char)k[i]))
return false;
}
return true;
}

/* There are too many arguments to this function - but it works */
static void encipher(int k, int i, char *pt, string plaintext)
{
int p, c;
if (islower((unsigned char)plaintext[i]))
{
p = plaintext[i] - 'a';
c = (p + k) % 26;
*pt = c + 'a';
}
else
{
p = plaintext[i] - 'A';
c = (p + k) % 26;
*pt = c + 'A';
}
}

int main(int argc, string argv[])
{
string k = argv[1];
if (argc < 2 || !isNumeric(k, strlen(k)))
return 1;
string plaintext = GetString();
int size = strlen(plaintext);
char ciphertext[size + 1];
int key = atoi(k);
for (int i = 0; i < size; i++)
{
if (isalpha(plaintext[i]))
{
encipher(key, i, &ciphertext[i], plaintext);
}
else
{
ciphertext[i] = plaintext[i];
}
}
ciphertext[size] = '\0';
printf("%s\n", ciphertext);
}

该程序名为 csr13,并提供以下输出:

$ csr13 4
The Quick Brown Fox Jumped Over The Lazy Dog
Xli Uymgo Fvsar Jsb Nyqtih Sziv Xli Pedc Hsk
$ csr13 22
Xli Uymgo Fvsar Jsb Nyqtih Sziv Xli Pedc Hsk
The Quick Brown Fox Jumped Over The Lazy Dog
$

encipher 函数的更好设计将传递单个字符加上“ key ”偏移量,并返回加密的字符:

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

static bool isNumeric(string k, int size)
{
for (int i = 0; i < size; i++)
{
if (!isdigit((unsigned char)k[i]))
return false;
}
return true;
}

static int encipher(int k, int c)
{
assert(isalpha(c));
if (islower(c))
return (c - 'a' + k) % 26 + 'a';
else
return (c - 'A' + k) % 26 + 'A';
}

int main(int argc, string argv[])
{
string k = argv[1];
if (argc < 2 || !isNumeric(k, strlen(k)))
return 1;
string plaintext = GetString();
int size = strlen(plaintext);
char ciphertext[size + 1];
int key = atoi(k);
for (int i = 0; i < size; i++)
{
if (isalpha(plaintext[i]))
ciphertext[i] = encipher(key, plaintext[i]);
else
ciphertext[i] = plaintext[i];
}
ciphertext[size] = '\0';
printf("%s\n", ciphertext);
}

关于c - 输出中出现奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663963/

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