i'm trying to make a crypt and decrypt program using a very simple criptography (just moving three letters foward in the alphabet). To do so, i need to use pointers and something is going wrong when i try to ask the user for the phrase he wants to cryptograph. here's the code, the error is on the "encrypt" function, in the scanf line.
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <ctype.h>
#define foreach(item, array) \
for(int keep = 1, \
count = 0,\
size = strlen(array); \
keep && count != size; \
keep = !keep, count++) \
for(item = (array) + count; keep; keep = !keep)
void* encrypt(char *phrase, char *alphabet);
void* decrypt(char *phrase, char *alphabet);
int main() {
setlocale(LC_ALL, "Portuguese");
char alphabet[27] = {"abcdefghijklmnopqrstuvwxyz"};
char phrase[100];
int menu = -1;
while(menu != 3)
{
printf("----------------------CAESAR'S CIPHER----------------------\n");
printf("1- Encrypt a text.-----------------------------------------\n");
printf("2- Decrypt a text.-----------------------------------------\n");
printf("3- Exit program.-------------------------------------------\n");
printf("Choose an action: ");
scanf("%d", &menu);
if(menu == 1)
{
encrypt(phrase, alphabet);
}
else if(menu == 2)
{
decrypt(phrase, alphabet);
}
}
}
void* encrypt(char *phrase, char *alphabet)
{
printf("TYPE THE PHRASE YOU'D LIKE TO ENCRYPT TO CAESAR'S CIPHER: ");
gets(phrase);
printf("HERE'S THE ENCRYPTED TEXT: ");
foreach(char *p, phrase) {
printf("%v", *p);
*p = tolower(*p);
foreach(char *a, alphabet) {
if (*p == *a) {
if(*p != 'X' && *p != 'Y' && *p != 'Z')
{
a += 3;
printf("%c", *a);
}
else if(*p == 'X')
{
printf("A");
}
else if(*p == 'Y')
{
printf("B");
}
else if(*p == 'Z')
{
printf("C");
}
}
}
}
}