Sunday, August 8, 2010

Caesar Cypher



You have intercepted the data transmission of the
enemy. It is known that the encryption being used is Caesar Cypher. In this
encryption technique, if the letter to be encrypted is the Nth letter in the alphabet,
replace it with the (N-K)th
where K is some fixed integer called the Key. This addition is done modulo 26.
The spaces in the message are not encrypted.



Input Specification



There will be a number of test cases. Each test case consists of an encrypted
message of maximum 200 characters and containing only uppercase letters and
spaces, and an integer k (1<=k<=1000) which is the key of the encryption.
End of file denotes the end of input.



Output Specification



Your output should be the decrypted message, each in a line by itself.



Example Input



LJMM PCBNB
1
LJMM PCBNB
53


Example Output



KILL OBAMA
KILL OBAMA
#include<iostream>
#include<string>
using namespace std;
int main()
{
          char A[300];
          int key,i=0,j=0;
          char d;
          while(gets(A))
          {
                    key=0;i=0;j=0;
                    cin>>key;
                    //Since the counting is cyclic therefore counting multiples of 26 would give the original alphabet.
                    key=key%26;
                    for(i=0;i<strlen(A);i++)
                   {
                             if(A[i]!=' ')
                            {
                                       for(j=0;j<key;j++)
                                      {
                                           //Condition for the encryption to count from Z once A is encountered.
                                                if(A[i]=='A')
                                               {
                                                          A[i]='Z';
                                                          continue;
                                               }
                                               A[i]=A[i]-1;
                                     }
                           }                       
                  }
                  cout<<A<<endl;
                  d=getchar();
         }
         return 0;
}

1 comment: