I am trying to encrypt a string by Caesar encryption, but it only works for one word (no spaces between characters). I would like to encrypt whole sentence. I have read here about getting white spaces from keyboard into a string by gets()
or fgets()
, but I cannot make it functional.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv) {
printf("Enter the shift: ");
int shift,index,length;
scanf("%d", &shift);
printf("Enter the string to encrypt: " );
char string[1000];
//gets(string);
scanf("%s",string);
//fgets(string,1000,stdin);
char encrypted[strlen(string) + 1];
for(index = 0, length = strlen(string); index < length; index++ ){
if (string[index]!=' ')
encrypted[index] = 'A' + (string[index] -'A' + shift) % 26;
else
encrypted[index]=' ';
}
encrypted[ strlen(string)] = '\0';
printf("%s\n", encrypted);
return (EXIT_SUCCESS);
}