首页 > 解决方案 > 循环更改不在循环内的字符串?

问题描述

我正在用 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);    
}

标签: carraysstringloopscs50

解决方案


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


推荐阅读