c - 循环更改不在循环内的字符串?
问题描述
我正在用 C 语言编写一个程序,该程序获取字符串ogplaintext
输入、密钥输入并输出密文。然而,这里展示的循环,而不是仅仅改变密文变量,也在改变ogplaintext
,这对我来说没有意义,因为ogplaintext
在循环中没有任何地方。(错误?)发生在操作“ciphertext[j] = argv[1][i]”之后,其中 argv[1] 是用户输入的密钥。对此有什么想法?
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) 而不是 =。
推荐阅读
- python - 尝试显示从 rgb 转换的灰度图像时出错
- database - SQLite3 查询 COUNT 组合两个表
- python - 如何将多个值添加到键字典
- django - Select2 仅将一个选择传递给 Django 中的表单
- spring - 如何从授权中获取 jwt 令牌
- jinja2 - jinja2.exceptions.TemplateSyntaxError:模板意外结束。Jinja 正在寻找以下标签:'endblock'
- java - ListLiterator 是 Iterator 的子类吗
- python - 从指向 CSV 的 URL 创建字典列表
- python-3.x - 如何使用熊猫删除重复值并保留任何一个
- haskell - 在 Haskell 中创建自定义标志