I want to write a simple encryption program that does the following: Take a file as input (redirected at the Unix prompt), and encrypt/decrypt and print each character as directed. Convert uppercase characters to the lowercase character 3 down in the alphabet, EXCEPT A, B, C which become x, y, z respectively. Convert lowercase characters to the uppercase character 2 down in the alphabet, EXCEPT a, b which become Y, Z respectively. Convert all punctuation characters (ASCII 33-47, 58-64, 91-96, and 123-126) to the punctuation character 1 down in the ASCII chart; e.g., 34 " becomes 33 !, 35 # becomes 34 ", 58 : becomes 47 /;
EXCEPT 33 ! which becomes 126 ~.
here is some sample output:
Code:
If the file which is redirected in contains the following text:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
01234567890
The quick brown fox jumped
over the lazy dogs.
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Then the output of the program should be::
xyzabcdefghijklmnopqrstuvw
YZABCDEFGHIJKLMNOPQRSTUVWX
01234567890
qFC OSGAI ZPMUL DMV HSKNCB
MTCP RFC JYXW BMEQ-
~!"#$%&'()*+,-./:;<=>?@[\]^_`{|}
now my code looks like:
Code:
#include <stdio.h>
#include <ctype.h>
#define TRUE 1;
#define False 0;
//function prototypes
char convert(char inputChar, int *whtSpc, int *charCnt);
void display(int numWords, int whtSpc);
char convUpper(char inputChar);
char convLower(char inputChar);
char convPunct(char inputChar);
int main(void)
{
//initialize vars
char inputChar;
char convertedChar;
int charCnt = 0;
int numWords = 0;
int whtSpc = 0;
//while((inputChar = getchar()) != EOF)
//{
convertedChar = convert(inputChar, &whtSpc, &charCnt);
//}//end while
display(numWords, whtSpc);
return 0;
}//end of main
char convert(char inputChar, int *whtSpc, int *charCnt)
{
char convertedChar;
while((inputChar = getchar()) != EOF)
{
if(isalnum(inputChar) == 1)
{
charCnt++;
} else if(isspace(inputChar) == 1) {
charCnt--;
}
if((charCnt >0) && (isspace(inputChar)))
{
whtSpc++;
}//end if
if(isupper(inputChar))
{
convertedChar = convLower(inputChar);
}//end if
else if(islower(inputChar))
{
convertedChar = convUpper(inputChar);
}//end if
else if(ispunct(inputChar))
{
convertedChar = convPunct(inputChar);
}
printf("%c", convertedChar);
}//end of while
return convertedChar;
}//end of convert()
/////////////////////////////////////////////////////////////////////////////
//Name: display()
//Purpose: displays the results of the conversion
/////////////////////////////////////////////////////////////////////////////
void display(int numWords, int whtSpc)
{
printf("\n%20s%20s", "Description", "Quantity");
printf("\n%20s%20s", "-----------", "--------");
printf("\n%20s%20d", "Words", numWords);
printf("\n%20s%20d", "Whitespace", whtSpc);
printf("\n\n");
}//end of display()
//////////////////////////////////////////////////////////////////////////////
//Name: convUpper()
//Purpose: converts lowercase chars to an uppercase char 2 down
//////////////////////////////////////////////////////////////////////////////
char convUpper(char inputChar)
{
char convertedChar = inputChar;
if(inputChar >= 97 && inputChar <=98)
{
if(inputChar == 97)
{
convertedChar = 'Y';
} else if(inputChar == 98){
convertedChar = 'Z';
}
}//end if
if(!(isupper(convertedChar)))
{
convertedChar = toupper(inputChar + 2);
}//end if
return convertedChar;
}//end of convUpper()
//////////////////////////////////////////////////////////////////////////////
//Name: convLower()
//Purpose: converts uppercase chars to a lowercase char 3 down
/////////////////////////////////////////////////////////////////////////////
char convLower(char inputChar)
{
char convertedChar = inputChar;
if(inputChar >= 65 && inputChar <= 67)
{
if(inputChar == 65)
{
convertedChar = 97;
} else if(inputChar == 66){
convertedChar = 98;
} else if(inputChar == 67){
convertedChar = 99;
}
}//end if
if(!(islower(convertedChar)))
{
convertedChar = tolower(inputChar + 3);
}//end if
return convertedChar;
}//end of convLower()
/////////////////////////////////////////////////////////////////////////////
//Name: convPunct()
//Purpose: wrap the puctioation arround + 1 times
/////////////////////////////////////////////////////////////////////////////
char convPunct(char inputChar)
{
char convertedChar;
if(inputChar == '!')
{
convertedChar = '~';
} else {
convertedChar = (int)inputChar - 1;
}
return convertedChar;
}//end of convPunct()
any help would be greatly appreciated and thank you in advance. This has been bugging me all night long.