Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Caesar cipher code explanation

$
0
0
Hi
I need to write a caeser cipher program in c and i came across an example that i did not really understand.

example:
#include <stdio.h>
#include <conio.h>
#include <string>


int main(){

int shift;
char message[81];

printf("Enter message to be encrypted: ");
scanf("%s", &message);
printf("Enter shift amount (1-25): ");
fflush(stdin);
scanf("%d", &shift);

for(int i=0;i<strlen(message);i++)
{
// checking for upper case
if((message[i]>='A')&&
(message[i]<='Z'))
message[i]=((message[i]-'A') + shift) % 26 + 'A';
else
//checking for lower case
if((message[i]>='a')&&
(message[i]<='z'))
message[i]=((message[i]-'a') + shift) % 26 + 'a';
}

printf("\n\n%s",message);


getch();
return 0;
}


could you pleas explain the following to me:
#include <conio.h>

fflush(stdin);

for(int i=0;i<strlen(message);i++)

if((message[i]>='A')&&
(message[i]<='Z'))
message[i]=((message[i]-'A') + shift) % 26 + 'A';

When i understand these, i will be able to write my own encryption program
thank you

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>