为什么我从c中的循环中得到随机输出?

为什么我从c中的循环中得到随机输出?,c,cs50,C,Cs50,我试图解决CS50课程pset2中的问题,当我运行该代码时: # include <stdio.h> # include <string.h> # include <cs50.h> # include <ctype.h> int main(int argc, string argv[]) { // check if key is exists if (argc != 2) { printf("%s

我试图解决CS50课程pset2中的问题,当我运行该代码时:

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

int main(int argc, string argv[])
{
    // check if key is exists
    if (argc != 2)
    {
        printf("%s", "usage: ./substitution key\n");
        return 1;
    }
    else
    {
        // key
        string ciphertext = argv[1]; // "VCHRPZGJNTLSKFBDQWAXEUYMOI"
        int cipher_length = strlen(ciphertext);
        string msg_error = "\0";
        int x = 0;

        if (cipher_length == 26)
        {
            int msg_num = 0;
            for (int i = 0; i < strlen(ciphertext); i++)
            {
                if (isdigit(ciphertext[i]))
                {
                    msg_num = 0; // false
                }
                else
                {
                    msg_num = 1; // true
                }
            }
            if (msg_num == 1)
            {

                string plaintext = get_string("plaintext: ");
                int n = strlen(plaintext);
                int a_letter = 'a';
                char plaintext_cp[n]; // copy from the original text
                char text[n];
                strcpy(plaintext_cp, plaintext);

                // to read the chars into plaintext
                for (int i = 0; i < n; i++)
                {
                    char lower_char = tolower(plaintext_cp[i]); // e.g -> d
                    if (lower_char >= 'a' && lower_char <= 'z')
                    {
                        int covert_to_ascii = lower_char; // 101
                        int index = covert_to_ascii - a_letter; // 101 - 98 = 4
                        if (islower(plaintext[i]))
                        {
                            text[i] = tolower(ciphertext[index]);
                        }
                        else
                        {
                            text[i] = toupper(ciphertext[index]);
                        }
                    }
                    else
                    {
                        text[i] = lower_char;
                    }
                }
                printf("ciphertext: %s\n", text);
                return 0;
            }
            else
            {
                printf("%s\n", "key must only contain alphabetic characters.");
                return 0;
            }
        }
        else
        {
            printf("%s\n", "Key must contain 26 characters.");
            return 1;
        }
    }
}
#包括
#包括
#包括
#包括
int main(int argc,字符串argv[])
{
//检查密钥是否存在
如果(argc!=2)
{
printf(“%s”,“用法:./substitution key\n”);
返回1;
}
其他的
{
//钥匙
字符串密文=argv[1];/“VCHRPZGJNTLSKFBDQWAXEUYMOI”
int cipher_length=strlen(密文);
字符串msg_error=“\0”;
int x=0;
如果(密码长度==26)
{
int msg_num=0;
for(int i=0;id

如果(lower_char>='a'&&lower_char查看您提供的屏幕截图,我不认为这是意外的输出。输出结果应该是这样的,但是,当您测试并按住CTRl+V组合键进入控制台时,您对IDE的速度太快,在转到新行执行新的输入命令之前,字符串输出紧随其后;或者只是意外地按了一下键,您没有注意到。否则,每次运行命令时,它都会输出相同的字符(或者如果是缺陷),但似乎只发生在最后一次命令输入时

(在匆忙或不注意时,我已经对自己这样做过好几次)

(//检查键是否存在)下的“printf”命令也有一个%s(字符串指针),但printf命令中没有字符串,因此不需要


文件末尾的最后两个printf似乎有类似的问题。除非您计划调用字符串变量,否则不需要%s。

char-plaintext\u-cp[n];
==>
char-plaintext\u-cp[n+1];
以包含
'\0'
printf(“用法…”)的空间;返回1;
不正确。如果要打印错误消息并返回非零,则应将消息写入stderr。如果将消息写入stdout,则不将消息视为错误,应返回0。IOW,执行
fprintf(stderr…);返回1;
printf(…);返回0;
.IMO,用法声明不是错误消息,您应该选择后者。我试图理解n+1的意义,因为n是0索引,所以如果我将其设为n+1,我认为它将忽略第一个字符,因此请告诉我为什么以及如何执行此操作?@pmgImagine您的文本是
“Hello”
。然后您就有了
strlen(text)==5
…但是字符串所需的空间是6个字节,因为
'\0'
副本也必须计算在内。因此
char copy[strlen(text)+1];/*用于'\0'*/strcpy(copy,text);
--执行
char foo[4];
这4个元素是
foo[0]
foo[1]
,和
foo[2]
foo[3]
。好吧,现在我明白了这一点,但它对我不起作用