简体   繁体   English

用户输入的凯撒密码不希望打印

[英]Undesired printing of Caesar Cipher with input from the user

I am new to programming.我是编程新手。 And I have elementary doubt in regard of strings.我对字符串有基本的怀疑。
I can successfully print a string given by a user.我可以成功打印用户给出的字符串。 But things get weird when I run the code for Caesar Cipher.但是当我运行 Caesar Cipher 的代码时,事情变得很奇怪。
Following codes will explain where actually the problem is arising.以下代码将解释问题实际出现的位置。

In the following code I am able to successfully print the string as enetered by me:在以下代码中,我能够成功打印我输入的字符串:

#include <stdio.h>
int main()
{
    char str[20];
    printf("Enter a string\n");
    scanf("%[^\n]s",&str);
    printf("The entered string is %s",str);
}

Output: Output: 在此处输入图像描述

OR (Using for loop as follows)(使用 for 循环如下)

#include <stdio.h>
int main()
{
    char str[20],i;
    printf("Enter a string\n");
    scanf("%[^\n]s",&str);
    printf("The entered string is:");
    for(i=0;i<=20;i++)
    {
        if(str[i]=='\0')
        break;
        printf("%c",str[i]);
    }
}

OUTPUT: OUTPUT: 在此处输入图像描述

I understand that at the end of every string \0 is present to mark the end of a character string.我知道在每个字符串的末尾都有 \0 来标记字符串的结尾。
But things go out of the way if I try to print Caesar Cipher in the above said methods.( Here I am declaring a character array of length 20,as I was told to do so in my Lab session. I have attached my actual lab question at the end )但是,如果我尝试在上述方法中打印凯撒密码,那么 go 的事情就会消失。(这里我声明了一个长度为 20 的字符数组,正如我在实验室 session 中被告知的那样。我附上了我的实际实验室最后的问题
1st method第一种方法

#include <stdio.h>
int main()
{
  char str[20],shift_str[20];
    int n;
    printf("Enter a string\n");
     scanf("%[^\n]s",&str);
     printf("Enter a number to shift the string:");
     scanf("%d",&n);
    int i,s,p;
    for(i=0;i<=20;i++)
    {
        if(str[i]=='\0')
        break;
        s=(int)str[i];//type casting str to get the ASCII Value
        p=n+s;
        shift_str[i]=(char)p;// type casting the shifted ASCII Value to char
        
    }
    printf("Caesar Cipher is:%s",shift_str);
    
}

OUTPUT: OUTPUT: 在此处输入图像描述

2nd Method(Using for loop):第二种方法(使用for循环):

#include <stdio.h>
int main()
{
  char str[20],shift_str[20];
    int n;
    printf("Enter a string\n");
     scanf("%[^\n]s",&str);
     printf("Enter a number to shift the string:");
     scanf("%d",&n);
    int i,s,p;
    for(i=0;i<=20;i++)
    {
        if(str[i]=='\0')
        break;
        s=(int)str[i];//type casting str to get the ASCII Value
        p=n+s;
        shift_str[i]=(char)p;// type casting the shifted ASCII Value to char
        
    }
    for(i=0;i<=20;i++)
    {
      if(shift_str[i]=='\0')
      break;
      printf("%c",shift_str[i]);
    }
}

在此处输入图像描述

As you can see the coding is running fine expect for the fact that it is printing some junk value stored in the array after the desired output.如您所见,编码运行良好,因为它在所需的 output 之后打印存储在数组中的一些垃圾值。
As far as I have learned, shift_str should have '\0' after "Ifmmp,Xpsme" so the string should terminate after it, but rather it goes on to print the junk values in the rest of the array as well!!据我所知, shift_str应该在“Ifmmp,Xpsme”之后有 '\0',所以字符串应该在它之后终止,而是继续打印数组的 rest 中的垃圾值!
So at first I would like to know why is it working in that way, and second what is the correct code to run my Caesar Cipher.所以首先我想知道为什么它会以这种方式工作,其次是什么是运行我的凯撒密码的正确代码。
I would be glad to receive any help.我很乐意得到任何帮助。
(I have originally posted this question in "code review stack", that later found that was not the correct place to ask this type of question as "code review" is meant for improving already successful coed.My apologies) (我最初在“代码审查堆栈”中发布了这个问题,后来发现这不是提出此类问题的正确位置,因为“代码审查”是为了改进已经成功的男女同校。我很抱歉)

My original lab question:我最初的实验室问题: 在此处输入图像描述

I understand that at the end of every string \0 is present to mark the end of a character string.我知道在每个字符串的末尾都有 \0 来标记字符串的结尾。

It doesn't seem like you understand that because you didn't put such a marker at the end of shift_str but then used it as if it was a string.您似乎不理解这一点,因为您没有在shift_str的末尾放置这样的标记,而是将其用作字符串。 In your code, shift_str is not a string because there is no \0 at the end of it and therefore it is an error to pass it to printf through %s .在您的代码中, shift_str不是字符串,因为它的末尾没有 \0 ,因此通过%s将其传递给printf是错误的。

Perhaps change:也许改变:

        if(str[i]=='\0')
        break;

to

        if(str[i]=='\0')
        {
            shift_str[i]='\0';
            break;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM