1
#include <stdio.h>
#include <string.h>
void dec();
int main()
{
    char pt[50], ct[50];
    int key, i;
    printf("Enter the plain text: ");
    scanf("%[^\n]", pt);
    printf("Enter the key: ");
    scanf("%d", &key);
    for (i = 0; i < strlen(pt); i++)
    {
        if (pt[i] >= 65 && pt[i] <= 90)
        {
            ct[i] = ((((pt[i] - 65)) + key) % 26) + 65;
        }
        else if (pt[i] >= 97 && pt[i] <= 127)
        {
            ct[i] = ((((pt[i] - 97)) + key) % 26) + 97;
        }
        else
        {
            ct[i] = pt[i];
        }
    }
    ct[i] = '\0';
    printf("encrypted text: ");
    printf("%s\n", ct);
    dec();
}
void dec()
{
    char st[50], kt[50];
    int key, i;
    printf("Enter the plain text: ");
    scanf(" %49[^\n]", st);
    printf("Enter the key: ");
    scanf("%d", &key);
    for (i = 0; i < strlen(st); i++)
    {
        if (st[i] >= 65 && st[i] <= 90)
        {
            kt[i] = ((((st[i] - 65)) - key) % 26) + 65;
        }
        else if (st[i] >= 97 && st[i] <= 127)
        {
            kt[i] = ((((st[i] - 97)) - key) % 26) + 97;
        }
        else
        {
            kt[i] = st[i];
        }
    }
    kt[i] = '\0';
    printf("encrypted text: ");
    printf("%s\n", kt);
}

The output is:

encrypted text: flskhu         
Enter the plain text: Enter the key: 3     
encrypted text: `s��xh 

In this output I am not able to give input to plain text for decryption. I face this issue many times when I try to take input two times using scanf.

Can anyone provide me any explanation to this problem?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    Remember that `Enter` key you ended all your inputs with? It will be added to the `stdin` buffer as a newline (`'\n'`). So after the call `scanf("%d",&key)` in the `main` function, that newline will be the first seen by `scanf("%[^\n]",st)` in the `dec` function. Unlike most other formats, `%[]` does *not* skip leading white-space. – Some programmer dude Apr 08 '21 at 10:46
  • That cannot be the real output. It cannot possibly print "encrypted text" before "enter the plain text" – klutt Apr 08 '21 at 11:00
  • @ANSHUMAN MISHRA note the one usually shouldn't correct the code in the questions because it may confuse future readers. I did not edit it out, but it's something to remember. – anastaciu Apr 08 '21 at 12:32

1 Answers1

1

As Some programmer dude stated, when you parse the key a \n character is left on the buffer and will be read by the next scanf the quick fix is to place a space before the specifier:

 scanf(" %49[^\n]", st);
        ^

Note that I also added a width limit, this avoids buffer overflow in case the inputed string is larger than what the container can take.


Another thing you should fix, the line:

else if (st[i] >= 97 && st[i] <= 127);
                        ^^^^^^^^^^^^

Is probably not what you want, besides being always true(the highlighted part), because the upper limit of char is 127, this will also include non alphabetic characters, see https://www.ascii-code.com/. Also note that 127 is the ASCII code for Del.

What you should do, for more portable code, is to use the character literals as limits:

else if (st[i] >= 'a' && st[i] <= 'z');
anastaciu
  • 23,467
  • 7
  • 28
  • 53