I'm a newbie in C and I just make this program for fun. But then I encountered a problem.
the encryptedWord
seems to erase its value every time system("cls")
occurs. Does it supposed to happen that way? and if it does, do you have any suggestion to substitute it?
Here's my code:
const char * dencrypt(int numeric, const char * encryptedWord);
int getKey();
void displayKey(int number, char *keyUp, char *keyLow, int num, char *keys);
int main(void) {
int ans = 'y';
int choice;
const char * encryptedWord = " ";
int count = 1;
while (ans == 'y' || ans == 'Y') {
system("cls");
printf("This program will encrypt and decrypt message according\n");
printf("to your choice.\n\n");
puts ("Pick your choice..");
puts ("1. Encrypt");
puts ("2. Decrypt");
printf("\n");
printf("Enter choice: ");
scanf("%d", &choice);
getchar();
while (choice < 1 || choice > 2) {
puts("Sorry kid, only 1 or 2.");
printf("Enter choice: ");
scanf("%d", &choice);
getchar();
}
switch(choice) {
case 1:
system("cls");
encryptedWord = dencrypt(choice, encryptedWord);
break;
case 2:
if (count > 1) {
system("cls");
printf("\nDo you want to use your last encrypted word?\n");
printf("Note that if you pick NO, your encrypted word\nwill be erased from the memory..");
printf("\n1. YES");
printf("\n2. NO");
printf("\nInput choice: ");
scanf("%d", &choice);
getchar();
system ("cls");
while(choice < 1 || choice > 2) {
printf("\nPfft.. pick only 1 or 2.");
printf("\nInput choice: ");
scanf("%d", &choice);
getchar();
}
switch (choice) {
case 1:
printf("\nLast Encrypted Word: %s\n", encryptedWord);
break;
case 2:
break;
}
} else {
system("cls");
}
dencrypt(choice, encryptedWord);
break;
}
printf("\n\nDo you want to continue?\n");
printf("Note that in doing so the console will be cleared");
printf("\nPress Y to continue or any other key to end this program.\n");
ans = getchar();
getchar();
count++;
}
}
const char * dencrypt(int numeric, const char * rWord) {
int keyStr, num,i, s, total;
int c;
bool found;
char word[SIZE];
char fWord[SIZE];
char keyUp[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char keyLow[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char keys[25];
switch (numeric) {
case 1:
puts("Input key for encryption");
break;
case 2:
puts("Input key for decryption");
break;
}
num = getKey() - 'A';
displayKey(numeric , keyUp, keyLow, num, keys);
printf("\n\n");
switch(numeric) {
case 1:
puts("Input a word to be encrypted");
break;
case 2:
puts("Input a word to be decrypted");
break;
}
i = 0;
total = 0;
while(i < SIZE - 1 && (c = getchar()) != '\n') {
word[i++] = c;
total++;
}
word[i] = '\0';
printf("\n");
puts("Original Word: ");
printf("%s\n\n", word);
switch(numeric) {
case 1:
printf("Encrypted word: \n");
for (i = 0; i <= total - 1; i++) {
found = false;
s = 0;
if (islower(word[i])) {
while (found != true) {
if (word[i] == keyLow[s]) {
found = true;
} else {
s++;
}
}
s += num;
if (s > 25) {
s -= 26;
}
printf("%c", keyLow[s]);
fWord[i] = keyLow[s];
} else if (isupper(word[i])) {
while (found != true) {
if (word[i] == keyUp[s]) {
found = true;
} else {
s++;
}
}
s += num;
if (s > 25) {
s -= 26;
}
printf("%c", keyUp[s]);
fWord[i] = keyUp[s];
} else if(isspace(word[i])) {
printf(" ");
fWord[i] = ' ';
} else {
printf("%c", word[i]);
fWord[i] = word[i];
}
}
printf("\n\n\n");
rWord = fWord;
break;
case 2:
printf("Decrypted word: \n");
for (i = 0; i <= total - 1; i++) {
found = false;
s = 0;
if (islower(word[i])) {
while (found != true) {
if (word[i] == keyLow[s]) {
found = true;
} else {
s++;
}
}
s -= num;
if (s < 0) {
s += 26;
}
printf("%c", keyLow[s]);
} else if (isupper(word[i])) {
while (found != true) {
if (word[i] == keyUp[s]) {
found = true;
} else {
s++;
}
}
s -= num;
if (s < 0 ) {
s += 26;
}
printf("%c", keyUp[s]);
} else if(isspace(word[i])) {
printf(" ");
} else {
printf("%c", word[i]);
}
}
printf("\n\n\n");
break;
}
return rWord;
}