-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchEnd.c
41 lines (38 loc) · 846 Bytes
/
matchEnd.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
// Checks if 1st string ends with 2nd string
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
bool matchEndString(char *str, char *ending);
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Usage: %s str1 str2\n", argv[0]);
return 1;
}
if (matchEndString(argv[1], argv[2]))
{
printf("%s ends with %s\n", argv[1], argv[2]);
}
else
{
printf("%s does not end with %s\n", argv[1], argv[2]);
}
return 0;
}
bool matchEndString(char *str, char *ending)
{
int n1 = strlen(str), n2 = strlen(ending);
if (n1 < n2)
{
return false;
}
for (int i = n1 - 1, j = n2 - 1; j >= 0; i--, j--)
{
if (str[i] != ending[j])
{
return false;
}
}
return true;
}