-
Notifications
You must be signed in to change notification settings - Fork 0
/
101-wildcmp.c
95 lines (81 loc) · 2.02 KB
/
101-wildcmp.c
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
88
89
90
91
92
93
94
95
#include "main.h"
int strlen_no_wilds(char *str);
void iterate_wild(char **wildstr);
char *postfix_match(char *str, char *postfix);
int wildcmp(char *s1, char *s2);
/**
* strlen_no_wilds - Returns the length of a string,
* ignoring wildcard characters.
* @str: The string to be measured.
*
* Return: The length.
*/
int strlen_no_wilds(char *str)
{
int len = 0, index = 0;
if (*(str + index))
{
if (*str != '*')
len++;
index++;
len += strlen_no_wilds(str + index);
}
return (len);
}
/**
* iterate_wild - Iterates through a string located at a wildcard
* until it points to a non-wildcard character.
* @wildstr: The string to be iterated through.
*/
void iterate_wild(char **wildstr)
{
if (**wildstr == '*')
{
(*wildstr)++;
iterate_wild(wildstr);
}
}
/**
* postfix_match - Checks if a string str matches the postfix of
* another string potentially containing wildcards.
* @str: The string to be matched.
* @postfix: The postfix.
*
* Return: If str and postfix are identical - a pointer to the null byte
* located at the end of postfix.
* Otherwise - a pointer to the first unmatched character in postfix.
*/
char *postfix_match(char *str, char *postfix)
{
int str_len = strlen_no_wilds(str) - 1;
int postfix_len = strlen_no_wilds(postfix) - 1;
if (*postfix == '*')
iterate_wild(&postfix);
if (*(str + str_len - postfix_len) == *postfix && *postfix != '\0')
{
postfix++;
return (postfix_match(str, postfix));
}
return (postfix);
}
/**
* wildcmp - Compares two strings, considering wildcard characters.
* @s1: The first string to be compared.
* @s2: The second string to be compared - may contain wildcards.
*
* Return: If the strings can be considered identical - 1.
* Otherwise - 0.
*/
int wildcmp(char *s1, char *s2)
{
if (*s2 == '*')
{
iterate_wild(&s2);
s2 = postfix_match(s1, s2);
}
if (*s2 == '\0')
return (1);
if (*s1 != *s2)
return (0);
return (wildcmp(++s1, ++s2));
}