mirror of
https://gitlab.gwdg.de/j.hahn02/university.git
synced 2026-01-01 14:54:25 -05:00
cleanup
This commit is contained in:
46
S1/GdCP/klausur_c_1/strmatch.c
Normal file
46
S1/GdCP/klausur_c_1/strmatch.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user