gpt4 book ai didi

c - 循环改变不在循环内部的字符串?

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

我正在用 C 编写一个程序,该程序获取字符串 ogplaintext 输入、 key 输入并输出密文。然而,这里展示的循环不仅改变了 ciphertext 变量,还改变了 ogplaintext ,这对我来说没有意义,因为 ogplaintext 不在环形。(错误?)发生在“ciphertext[j] = argv[1][i]”操作之后,其中argv[1]是用户输入的 key 。对此有何想法?

string ciphertext = ogplaintext;
int j = 0;
printf("ogplaintext: %s ; initiating ciphertext conversion \n", ogplaintext);
// convert j'th digit of ciphertext in key
while(j < strlen(ciphertext))
{
for(int i = 0; i < 25; i++)
{
if(i == editplaintext[j])
{
ciphertext[j] = argv[1][i];
j++;
printf("after ciphertext character change ogplaintext: %s\n", ogplaintext);
}
else if(editplaintext[j] == 27)
{
j++;
}
}
}

如果有任何帮助,我会将整个代码粘贴到此处:

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

int main(int argc, string argv[])
{
if(argc < 2)
{
printf("Usage: ./substitution key");
return 1;
}
else
{
if(strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.");
return 1;
}
for(int i = 0; i < 25; i++)
{
if(argv[1][i] < 65 || (argv[1][i] > 90 && argv[1][i] < 97) || argv[1][i] > 122)
{
printf("Usage: ./substitution key");
return 1;
}
}
}
string ogplaintext = get_string("plaintext: ");
string plaintext = ogplaintext;
int editplaintext[strlen(ogplaintext)];
for(int i = 0; i < strlen(ogplaintext); i++)
{
// if oglaintext[i] is uppercase
if(ogplaintext[i] >= 65 && ogplaintext[i] <= 90)
{
// convert in digit from 0 to 25
editplaintext[i] = ogplaintext[i] - 65;
}
// if ogplaintext[i] is lowercase
else if(ogplaintext[i] >= 97 && ogplaintext[i] <= 122)
{
// convert in digit from 0 to 25
editplaintext[i] = ogplaintext[i] - 97;
}
// if ogplaintext[i] is not alphabetical
else
{
editplaintext[i] = 27;
}

}

string ciphertext = ogplaintext;
int j = 0;
printf("ogplaintext: %s ; initiating ciphertext conversion \n", ogplaintext);
// convert j'th digit of ciphertext in key
while(j < strlen(ciphertext))
{
for(int i = 0; i < 25; i++)
{
if(i == editplaintext[j])
{
ciphertext[j] = argv[1][i];
j++;
printf("after ciphertext character change ogplaintext: %s\n", ogplaintext);
}
else if(editplaintext[j] == 27)
{
j++;
}
}
}
printf("ogplaintext: %s ; finalized ciphertext conversion \n", ogplaintext);
for( int i = 0; i < strlen(ciphertext); i++)
{
// if ith character of ogplaintext is uppercase and ith character of ciphertext is lowercase
if((ogplaintext[i] >= 65 && ogplaintext[i] <= 90) && (ciphertext[i] >))
{
ciphertext[i] = (ciphertext[i] - 32);
}
// else if ith character of ogplaintext is lowercase and ith character of ciphertext is uppercase
else if((ogplaintext[i] >= 97 && ogplaintext[i] <= 122) && (ciphertext[i] >= 65 && ciphertext[i] <= 90))
{
ciphertext[i] = (ciphertext[i] + 32);
}
else
{
printf("c");
}
}
printf("ciphertext: %s", ciphertext);
}

最佳答案

使用 strcpy(ciphertext, ogplaintext) 而不是 =。

关于c - 循环改变不在循环内部的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60027939/

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