The code takes input(the string) to be encoded and a key from the user such that when the key is added to the input the input is increased by the amount of the given key . For ex. if the key is 2, so the input A changes to C, b changes to d, and so on.
I have written a code for the same but cannot get the output.
int main()
{
int x,i,y,c;
char text[20];
printf("enter the plaintext:");
gets(text);
printf("enter the key: ");
scanf("%d",&x);
for (y=0;y<strlen(text);y++)
{
if (text[i]>='a'&&text[i]<='z'&&text[i]>='A'&&text[i]<='Z' )
{
int c=(int)text[i]+x;
printf("%c\n",text[i]);
}
}
}
The result that i am getting is blank. kindly help me.
There are a lot of problems in your proposal, it is needed to check the inputs success, you iterate on y rather than on i, you compute the new char code but you do not print it
Here a corrected proposal :
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char text[100];
printf("enter the plaintext:");
if (fgets(text, sizeof(text), stdin) != NULL) {
int key;
printf("enter the key: ");
if (scanf("%d", &key) == 1) {
int i;
for (i=0; text[i] != 0; ++i)
{
if (isalpha(text[i]))
putchar(text[i] + key); /* may not be printable */
else
putchar(text[i]);
}
}
}
return 0;
}
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra c.c
pi@raspberrypi:/tmp $ ./a.out
enter the plaintext:the sentence to encode
enter the key: 3
wkh vhqwhqfh wr hqfrgh
pi@raspberrypi:/tmp $ ./a.out
enter the plaintext:Alea jacta est
enter the key: 2
Cngc lcevc guv
For the fun, 32 is not a very good key to encode uppercase characters :
pi@raspberrypi:/tmp $ ./a.out
enter the plaintext:THE FIVE BOXING WIZARDS JUMP QUICKLY.
enter the key: 32
the five boxing wizards jump quickly.