Saturday, July 27, 2013

Rail Fence Cipher

//rail_fence_cipher
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int j,k;
    string as, bs, cipherText;
    //freopen("input.txt","r",stdin);
    while(cin>>cipherText){
        int len = cipherText.size();
        int flag = 0;
        for(int i=0; i < len; i++){
            if(i == ceil(len / 2.0)) flag = 1;
            if(flag == 0) as += cipherText[i];
            else bs += cipherText[i];
        }
        j = k = 0;
        cout<<"Cipher Text: "<<cipherText<<endl;
        cout<<"Plain Text: ";
        for(int i=0; i < len; i++){
            if(i % 2 == 0) cout<<as[k++];
            else cout<<bs[j++];
        }
        puts("");
        //clearing
        as.clear();
        bs.clear();
    }
    return 0;
}

No comments:

Post a Comment