简体   繁体   中英

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.
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: 在此处输入图像描述

OR (Using for loop as follows)

#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: 在此处输入图像描述

I understand that at the end of every string \0 is present to mark the end of a character string.
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 )
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: 在此处输入图像描述

2nd Method(Using for loop):

#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.
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!!
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.

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. 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 .

Perhaps change:

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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