1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <stdio.h>
#include <stdlib.h>
#define TAILLE_MAX 100
int masque(char *motif, char *string);
int main(int argc, char *argv[])
{
FILE * fichier = fopen(argv[1],"r");
char chaine[TAILLE_MAX];
char * Tab[TAILLE_MAX];
char * motif;
char * string;
int i=0;
if (fichier == NULL)
{
printf("Erreur a l'ouverture\n");
return 1;
}
if (fichier != NULL)
{
while (fgets(chaine, TAILLE_MAX, fichier) != NULL) // On lit le fichier tant qu'on ne reçoit pas d'erreur (NULL)
{
Tab[i]=chaine;
i++;
}
fclose(fichier);
}
printf("Entrez votre motif de recherche : "); // On demande le motif à l'utilisatuer
scanf("%s", motif);
for (i=0; i<TAILLE_MAX; i++)
{
string = Tab[i];
if(masque(motif, string)==1) printf("%s", string); // On affiche le mot s'il correspond au motif
}
}
int masque(char *motif, char *string)
{
const char *cc = NULL, *mc = NULL;
while ((*string) && (*motif != '*'))
{
if ((*motif != *string) && (*motif != '?')) return 0;
motif++;
string++;
}
while (*string)
{
if (*motif == '*')
{
if (!*++motif) return 1;
mc = motif;
cc = string+1;
}
else if ((*motif == *string) || (*motif == '?'))
{
motif++;
string++;
}
else
{
motif = mc;
string = cc++;
}
}
while (*motif == '*')
{
motif++;
}
return !*motif;
} |
Partager