-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path101-wildcmp.c
executable file
·108 lines (99 loc) · 2.21 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
96
97
98
99
100
101
102
103
104
105
106
107
#include "main.h"
int bandersnatch(char *s1, char *s2);
char *move(char *s2);
/**
* wildcmp - compares two strings recursively,
* checking for wildcards expansion
* @s1: first string to compare
* @s2: second string to compare
* Return: 1 if the strings can be considered identical
* otherwise 0
*/
int wildcmp(char *s1, char *s2)
{
/**
* this is going to be a sum of return values
*/
int sum = 0;
/**
* if we reach the end of s1 and the char in s2 is a *
* and if the next chars of s2 are *, return 1
*/
if (*s1 == '\0' && *s2 == '*' && !*move(s2))
return (1);
/**
* if the chars are equal in both strings,
* if we reached the end of s1, return 1
* else increment s1 and s2 by 1
*/
if (*s1 == *s2)
{
if (*s1 == '\0')
return (1);
return (wildcmp(s1 + 1, s2 + 1));
}
/**
* if we reached the end of both strings,
* return 0
*/
if (*s1 == '\0' || *s2 == '\0')
return (0);
/**
* if the char in s2 is a *
* finds the address of the first char after the *
* if we reached the end of s2, return 1
* if the chars are equal, add the return values
* of wildcmp() to sum
* add the return value of bandersnatch() to sum
* convert non-zero to 1, keeps 0 at 0, return
*/
if (*s2 == '*')
{
s2 = move(s2);
if (*s2 == '\0')
return (1);
if (*s1 == *s2)
sum += wildcmp(s1 + 1, s2 + 1);
sum += bandersnatch(s1 + 1, s2);
return (!!sum);
}
return (0);
}
/**
* bandersnatch - checks recursively for all the paths when the
* characters are equal
* @s1: first string
* @s2: second string
* Return: return value of wildcmp() or of itself
*/
int bandersnatch(char *s1, char *s2)
{
/**
* if we reached the end of s1, return 0
* if chars are equal, return the return value of wildcmp()
* increment s1 by 1, not s2
*/
if (*s1 == '\0')
return (0);
if (*s1 == *s2)
return (wildcmp(s1, s2));
return (bandersnatch(s1 + 1, s2));
}
/**
* *move - moves the current char past the *
* @s2: string to iterate over
* Return: the address of the character after the *
*/
char *move(char *s2)
{
/**
* if the current char is a *
* increment s2 by 1
* else return the address of
* the first char past all *
*/
if (*s2 == '*')
return (move(s2 + 1));
else
return (s2);
}