I'm just learning how to do C programming, and so far its been quite easy since i programmed for some games before, but tonight im a little tired, and stumped. Ive been trying to do this lab for my class. It is supposed to prompt the user for a string, and be able to encode, or decode the string. It seems to be encoding fine, but decoding wont work. I can't seem to figure it out, can you please help? Heres the code i have so far...so close i think! Thanks for your time.
Code:
// Dependencies -------------------------------------------------------------
#include <stdio.h> // For input/output functions.
#include <string.h> // For string functions.
// End dependencies ---------------------------------------------------------
// Functions ----------------------------------------------------------------
/* --------------------------------------------------------------------------+
main - Displays a menu for selection of printing the message, encoding a
message, or decoding a message
Returns
program status
Functional Notes
getchar() prevents auto closing of window by expecting an Enter keypress.
--------------------------------------------------------------------------*/
char in_msg[250] = "";
char out_msg[250] = "";
char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char decode[] = "HCFGBMNTVZYPKASQUDXIRWEJLOhcfgbmntvzypkasqudxirwejlo";
char t[250];
int n;
int j;
int i;
int menu_item = 0;
// ----------------------------------------------------------------------
// Display course work items menu.
// ----------------------------------------------------------------------
int main()
{
do
{
printf("\t***Lab 09 - Encode/Decode***\n");
printf("\t***Please choose one of the following functions...***\n");
printf("\tdecrypt <1>\n");
printf("\tencrypt <2>\n");
printf("\tquit: <3>\n");
scanf("%d%*c", &menu_item);
switch (menu_item)
{
case 1 :
{
strcpy (t, in_msg);
n = strlen (in_msg);
for ( j = 0 ; j < 53 ; j++)
{
for (i = 0 ; i < n ;i++)
{
if ( t[i] == encode[j] )
{
t[i] = decode [j];
}
}
}
strcpy (out_msg, t);
printf("***********************************************************************\n");
printf("Original Message: %s\n", in_msg);
printf("Decrypted Message: %s\n", out_msg);
printf("***********************************************************************\n");
strcpy (in_msg, out_msg);
menu_item = 0;
break;
}
case 2 :
{
printf("Please input a new message: ");
gets(in_msg);
strcpy (t, in_msg);
n = strlen (in_msg);
for ( j = 0 ; j < 53 ; j++)
{
for(i = 0 ; i < n ;i++)
{
if (t[i] == decode[j])
{
t[i] = encode [j];
}
}
}
strcpy (out_msg, t);
printf("***********************************************************************\n");
printf("Original Message: %s\n", in_msg);
printf("Decrypted Message: %s\n", out_msg);
printf("***********************************************************************\n");
strcpy (in_msg, out_msg);
menu_item = 0;
break;
}
case 3 :
{
return 0;
}
default:
{
printf("Not a valid menu choice, please try again");
break;
}
}
} while (menu_item == 0 );
// ----------------------------------------------------------------------
// Standard DOS window exit for when running outside the IDE.
// ----------------------------------------------------------------------
getchar(); // Wait for Enter keypress.
return 0;
}
// End functions ---------------------------------------------------------