#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?