Created
September 10, 2017 16:18
-
-
Save BigZ/535a22493a664020db3f20192057e523 to your computer and use it in GitHub Desktop.
match in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
int match(char *str1, char *str2) | |
{ | |
int i, j; | |
// on va jusqu'au bout de la deuxieme chaine | |
for (i = 0, j = 0; str2[j] != '\0'; i++, j++) { | |
// si la premiere chaine est finie avant la deuxieme c'est dead | |
if (str1[i] == '\0') { | |
return 0; | |
} | |
// si ls deux caracteres acutels sont egaux on passe au suivant | |
if (str1[i] == str2[j]) { | |
continue; | |
} | |
// si c'est une * on cherche le prochain carac de str1 qui match le carac d'apres de str2 | |
if (str2[j] == '*') { | |
j++; | |
while (str1[i] != str2[j]) { | |
i++; | |
// on est arrivés au bout de str1 sans trouver le carac apres l'*, c'est dead | |
if (str1[i] == '\0') { | |
return 0; | |
} | |
} | |
} | |
} | |
// chaine déroulée jusqu'au bout, ok | |
return 1; | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("%d\n", match("test", "test")); | |
printf("%d\n", match("test", "test2")); | |
printf("%d\n", match("test", "t*t")); | |
printf("%d\n", match("test42", "t*t*2")); | |
printf("%d\n", match("test42", "t*t*3")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment