Wed Jan 23, 2013 2:11 am
#include <stdio.h>
#include <string.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;
int shift_value;
printf( "Doing encrypt\n" );
printf("\nPlease enter the text you wish to encrypt CAPS LOCK ONLY: ");
fgets(buff, sizeof(buff), stdin);
printf("\nEnter your encryption shift value (anything from +1 to 25): ");
scanf("%i", &shift_value);
while(buff[i] != '\0'){
if(buff[i] >= 'A' && buff[i] <= 'Z'){
buff[i] = 'A' + (buff[i] - 'A' + shift_value) % 26;
}
i++;
}
printf("\n Your encrypted text is: %s \n", buff);
}
void decode(void){
char buff[BUFSIZ];
int i = 0;
int shift_value;
printf( "Doing decrypt\n" );
printf("\nPlease enter the text you wish to decrypt CAPS LOCK ONLY: ");
fgets(buff, sizeof(buff), stdin);
printf("\nEnter your encryption shift value (anything from +1 to 25): ");
scanf ("%i", &shift_value);
while(buff[i] != '\0'){
if(buff[i] >= 'A' && buff[i] <= 'Z'){
int c = buff[i] - 'A' - shift_value;
if(c < 0)
c += 26;
buff[i] = 'A' + c % 26;
}
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;
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.