mirror of
https://gitlab.gwdg.de/j.hahn02/university.git
synced 2026-01-01 06:44:25 -05:00
47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
#include<string.h>
|
|
#include<ctype.h>
|
|
|
|
int str_match(const char s[], const char p[], int strict) {
|
|
// takes string and pattern and strict flag
|
|
// should return if the characters in the pattern appear in the string in the same order
|
|
// strict should also check for difference in capital letters
|
|
// it does not matter whether there are characters in between (other)
|
|
|
|
int ls = strlen(s);
|
|
int lp = strlen(p);
|
|
int o = -1;
|
|
|
|
// Loop the patterns and the string for each pattern
|
|
for (const char *cp = p; *p != '\0'; p++) {
|
|
for (int j = 0; j < ls; j++) {
|
|
char chp = *p;
|
|
char cha = s[j];
|
|
|
|
if (!strict) {
|
|
chp = (char) tolower(chp); cha = (char) tolower(cha);
|
|
}
|
|
if (cha == chp) {
|
|
// Break condition
|
|
if (j < o) {
|
|
return 0;
|
|
}
|
|
|
|
// Set the new occurence
|
|
o = j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
#include<stdio.h>
|
|
|
|
int main() {
|
|
printf("%d\n", str_match("Hallo Welt", "alo", 1));
|
|
printf("%d\n", str_match("Hallo Welt", "alo", 0));
|
|
printf("%d\n", str_match("Hallo Welt", "alho", 1));
|
|
printf("%d\n", str_match("Hallo Welt", "alho", 0));
|
|
}
|
|
|