Caesar Cipher in C

 #include<stdio.h>

int main(){

char inputtext[]="Deep Raj Bhujel", ciphertext[100], decipheredtext[100];

int i,shift=3;

for(i=0;inputtext[i]!='\0';i++){

if(isupper(inputtext[i]))

ciphertext[i]=(inputtext[i]-65+shift)%26 + 65;

else if(islower(inputtext[i]))

ciphertext[i]=(inputtext[i]-97+shift)%26 + 97;

else

ciphertext[i]=inputtext[i];

}

ciphertext[i]='\0';

printf("The encrypted string is %s\n\n",ciphertext);

for(i=0;ciphertext[i]!='\0';i++){

if(isupper(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-65-shift)%26 + 65;

else if(islower(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-97-shift)%26 + 97;

else

decipheredtext[i]=ciphertext[i];

}

decipheredtext[i]='\0';

printf("The decrypted string is %s",decipheredtext);

return 0;

}

No comments:

Post a Comment