Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/String-matching/C/string-matching-1.c
Normal file
32
Task/String-matching/C/string-matching-1.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int startsWith(const char* container, const char* target)
|
||||
{
|
||||
size_t clen = strlen(container), tlen = strlen(target);
|
||||
if (clen < tlen)
|
||||
return 0;
|
||||
return strncmp(container, target, tlen) == 0;
|
||||
}
|
||||
|
||||
int endsWith(const char* container, const char* target)
|
||||
{
|
||||
size_t clen = strlen(container), tlen = strlen(target);
|
||||
if (clen < tlen)
|
||||
return 0;
|
||||
return strncmp(container + clen - tlen, target, tlen) == 0;
|
||||
}
|
||||
|
||||
int doesContain(const char* container, const char* target)
|
||||
{
|
||||
return strstr(container, target) != 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Starts with Test ( Hello,Hell ) : %d\n", startsWith("Hello","Hell"));
|
||||
printf("Ends with Test ( Code,ode ) : %d\n", endsWith("Code","ode"));
|
||||
printf("Contains Test ( Google,msn ) : %d\n", doesContain("Google","msn"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
44
Task/String-matching/C/string-matching-2.c
Normal file
44
Task/String-matching/C/string-matching-2.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* returns 0 if no match, 1 if matched, -1 if matched and at end */
|
||||
int s_cmp(const char *a, const char *b)
|
||||
{
|
||||
char c1 = 0, c2 = 0;
|
||||
while (c1 == c2) {
|
||||
c1 = *(a++);
|
||||
if ('\0' == (c2 = *(b++)))
|
||||
return c1 == '\0' ? -1 : 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* returns times matched */
|
||||
int s_match(const char *a, const char *b)
|
||||
{
|
||||
int i = 0, count = 0;
|
||||
printf("matching `%s' with `%s':\n", a, b);
|
||||
|
||||
while (a[i] != '\0') {
|
||||
switch (s_cmp(a + i, b)) {
|
||||
case -1:
|
||||
printf("matched: pos %d (at end)\n\n", i);
|
||||
return ++count;
|
||||
case 1:
|
||||
printf("matched: pos %d\n", i);
|
||||
++count;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
printf("end match\n\n");
|
||||
return count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
s_match("A Short String", "ort S");
|
||||
s_match("aBaBaBaBa", "aBa");
|
||||
s_match("something random", "Rand");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue