Interview Questions

191. Write code for Ceaser Cipher Algorithm to encrypt/decrypt messages.

Microsoft Interview Questions and Answers


(Continued from previous question...)

191. Write code for Ceaser Cipher Algorithm to encrypt/decrypt messages.

Question:
Write code for Ceaser Cipher Algorithm to encrypt/decrypt messages.


maybe an answer:


#include <stdio.h>
#include <string.h>

void encrypt_decrypt_String(int kShift, int pOption);
int main()
{
int option = 0;
int shift = 0;
printf("Enter the shift for caeser cipher..\n");
scanf("%d",&shift);
printf("Enter 1 to encrypt and 0 to decrypt..\n");
scanf("%d",&option);
while(getchar() != '\n');
encrypt_decrypt_String(shift, option);
}

void encrypt_decrypt_String(int kShift, int pOption)
{
char ch;
ch = getchar();
while(ch != '\n')
{
if(ch == ' ')
{
putchar(' ');
}
else
{
if(pOption == 1)
{
if(isupper(ch))
{
putchar('A' + (ch + kShift - 'A')%26);
}
else
{
putchar('a' + (ch + kShift - 'a')%26);
}
} else if(pOption == 0)
{
if(isupper(ch))
{
putchar('Z' + (ch - kShift - 'Z')%26);
}
else
{
putchar('z' + (ch - kShift - 'z')%26);
}
}
else
{
printf("Option not supported..\n");
break;
}
}
ch = getchar();
}
putchar(ch);
}

(Continued on next question...)

Other Interview Questions