S
Steve Clay
I have a small C program for a college course. It is meant to encrypt
and decrypt lower case letters and leave spaces as spaces. I can't
get it to run properly as I think I have a problem in the While
construct area. It is driving me mad trying to sort it out - can
someone please help.
The program is:
/*
* Program to encrypt a message using the Turbo C random
* number generator. Only lower case letters and the space
* character can be used in the message.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#define SEED 1
unsigned char encrypt(unsigned char);
unsigned char decrypt(unsigned char);
void wait(void);
main()
{
unsigned char c;
char type;
/*
* initialise the random number generator
*/
srand(SEED);
/*
* display user headings and prompts
*/
clrscr();
cprintf("T223 encryption program\r\n\r\n");
cprintf("Type d to decode a message or any other character to encode
");
type = getch(); /* input the user option */
cprintf("\r\nNow type in the message, use only lower case letters "
"or space.\r\nEnd with any other character.\r\n\r\n");
c = (unsigned char)getchar(); /* read in first character */
/*
* main encryption/decryption loop
* loop while characters are lower case letters or spaces
*/
while ((c>='a') && (c<='z') && (c=' '));
{
if (c==' ')
{
switch (type)
{
case '64':
c = decrypt (c) ;
break;
default:
c = encrypt (c) ;
break;
}
}
putchar('c'); /* display encrypted/decrypted character */
c = (unsigned char)getchar(); /* get next character */
}
/*
* end of main while loop
*/
wait();
}
/*
* end of main() function
*/
/*
* function encrypt()
* ==================
* encrypts a single character
*/
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter + r;
if (letter>'z')
{
letter = letter - 26;
}
return(letter);
}
/*
* end of encrypt()
*/
/*
* function decrypt()
* ==================
* decrypts a single character
*/
unsigned char decrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter - r;
if (letter<'a')
{
letter = letter + 26;
}
return(letter);
}
/*
* end of decrypt()
*/
/*
* function wait()
* ===============
* waits for a keypress
*/
void wait(void)
{
fflush(stdin);
cprintf("\r\nPress return to terminate");
getchar();
}
/*
* end of wait()
*/
/*
* end of program
*/
and decrypt lower case letters and leave spaces as spaces. I can't
get it to run properly as I think I have a problem in the While
construct area. It is driving me mad trying to sort it out - can
someone please help.
The program is:
/*
* Program to encrypt a message using the Turbo C random
* number generator. Only lower case letters and the space
* character can be used in the message.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#define SEED 1
unsigned char encrypt(unsigned char);
unsigned char decrypt(unsigned char);
void wait(void);
main()
{
unsigned char c;
char type;
/*
* initialise the random number generator
*/
srand(SEED);
/*
* display user headings and prompts
*/
clrscr();
cprintf("T223 encryption program\r\n\r\n");
cprintf("Type d to decode a message or any other character to encode
");
type = getch(); /* input the user option */
cprintf("\r\nNow type in the message, use only lower case letters "
"or space.\r\nEnd with any other character.\r\n\r\n");
c = (unsigned char)getchar(); /* read in first character */
/*
* main encryption/decryption loop
* loop while characters are lower case letters or spaces
*/
while ((c>='a') && (c<='z') && (c=' '));
{
if (c==' ')
{
switch (type)
{
case '64':
c = decrypt (c) ;
break;
default:
c = encrypt (c) ;
break;
}
}
putchar('c'); /* display encrypted/decrypted character */
c = (unsigned char)getchar(); /* get next character */
}
/*
* end of main while loop
*/
wait();
}
/*
* end of main() function
*/
/*
* function encrypt()
* ==================
* encrypts a single character
*/
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter + r;
if (letter>'z')
{
letter = letter - 26;
}
return(letter);
}
/*
* end of encrypt()
*/
/*
* function decrypt()
* ==================
* decrypts a single character
*/
unsigned char decrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter - r;
if (letter<'a')
{
letter = letter + 26;
}
return(letter);
}
/*
* end of decrypt()
*/
/*
* function wait()
* ===============
* waits for a keypress
*/
void wait(void)
{
fflush(stdin);
cprintf("\r\nPress return to terminate");
getchar();
}
/*
* end of wait()
*/
/*
* end of program
*/