I want to add a few things to this caesar cipher program but am not exactly sure how. They are:
1. Before encryption move the first letter of the message to the last posistion.( "frog" would be "rogf")
2.replace each letter by a constantly increasing value of k. So,
if k is 3, replace the first letter of the message by the letter that comes 3 positions after it. Next,
replace the second letter of the message by the letter that comes 4 positions after it. Next, replace
the third letter of the message by the letter that comes 5 positions after it, and so on.
3.At the beginning ask the user the value they want to use to encrypt the code. (It is currently set up at 3) But, i want it to be an option for the user.
4. Let the code "wrap around" when i type in zebra instead of returning cheud, it gives a funky character for z.
Not sure how to do these, they seem simple i just cant come up with how to do them.
Code:
#include <stdio.h>
void menu ( void ) {
printf("Please enter a number: \n"
"1-Encrypt\n"
"2-Decrypt\n"
"3-Exit\n"
"prompt > ");
fflush( stdout );
}
int getchoice ( void ) {
char buff[BUFSIZ];
int choice = 0;
do {
menu();
if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
/* success reading a line, does it make sense? */
if ( sscanf( buff, "%d", &choice ) != 1 ) {
printf( "Enter a number\n" );
}
} else {
/* user EOF, just exit now */
choice = 3;
}
} while ( choice < 1 || choice > 3 );
return choice;
}
void encode ( void )
{
char buff[BUFSIZ];
int i = 0;
printf( "Doing encrypt\n" );
printf("\nPlease enter the text you wish to encrypt: ");
fgets(buff, sizeof(buff), stdin);
{
while ( buff[i] != '\0' )
{
buff[i] = buff[i] + 3;
i++;
}
}
printf("\n Your encrypted text is: %s \n", buff);
}
void decode ( void )
{
char buff[BUFSIZ];
int i = 0;
printf( "Doing decrypt\n" );
printf("\nPlease enter the text you wish to decrypt: ");
fgets(buff, sizeof(buff), stdin);
{
while ( buff[i] != '\0' )
{
buff[i] = buff[i] - 3;
i++;
}
}
printf("\nYour decrypted text is: %s \n", buff);
}
int main ( ) {
int choice;
while ( (choice=getchoice()) != 3 ) {
if ( choice == 1 ) {
encode();
} else
if ( choice == 2 ) {
decode();
}
}
return 0;
}