I am new to C programming. Please i need your help with the following: after compiling & running the code i wrote(one time pad encryption) on devc++ it doesn't get the two strings:it takes one string , then it shows the program is not responding. I was asked to use the fgets function, not scanf or gets, but even so i tried the gets function. It was returning answers as NULL.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define clear_buffer while(getchar()!='\n');
char encrypt(char texte[100],char cle[100]){
char encrypted;
int i=0;
while(texte[i]!='\0' && cle[i]!='\0'){
encrypted=(texte[i]+cle[i])%26;
i++;
return encrypted;
}
}
char decrypt(char encrypted[100],char cle[100]){
char decrypted;
int i=0;
while(encrypted[i]!='\0' && cle[i]!='\0'){
decrypted=(encrypted[i]-cle[i])%26;
i++;
return decrypted;
}
}
int main()
{
char reponse,texte[100],cle[100],encrypted[100];
int i=0;
do{
printf("Voulez vous crypter ou decrypter un texte?(Ecrire C pour crypter et D pour decrypter)\n");
scanf("%c",&reponse);
}while (reponse!='C'&& reponse!='D'&& reponse!='c'&& reponse!='d');//controle pour obliger l'utilisateur à donner une reponse valide
if(reponse=='C'||reponse=='c'){
printf("Donner un texte a crypter\n");
fgets(texte,100,stdin);
while(texte[i]!=0)
i++;
if (i>0 && texte[i-1]!='\n')
clear_buffer;
printf("Donner une cle de meme taille\n");
fgets(cle,100,stdin);
i=0;
while(cle[i]!=0)
i++;
if (i>0 && cle[i-1]!='\n')
clear_buffer;
printf("Le texte crypte est:%s\n",encrypt(texte,cle));
}else{
printf("Donner un texte (deja crypte) à decrypter\n");
fgets(encrypted,100,stdin);
i=0;
while(encrypted[i]!=0)
i++;
if (i>0 && encrypted[i-1]!='\n')
clear_buffer;
printf("Donner la cle (deja utilisee pour crypter\n");
fgets(cle,100,stdin);
i=0;
while(cle[i]!=0)
i++;
if (i>0 && cle[i-1]!='\n')
clear_buffer;
printf("Le texte decrypte est:%s\n",decrypt(encrypted,cle));
}
system("pause");
return 0;
}