| version 1.3, 2001/10/10 17:59:53 |
version 1.4, 2001/10/21 16:44:22 |
| Line 8 char *strestr(const char *s1, const char |
|
| Line 8 char *strestr(const char *s1, const char |
|
| return strstr(s1,s2) == NULL ? NULL : strendstr(s1,s2); |
return strstr(s1,s2) == NULL ? NULL : strendstr(s1,s2); |
| } |
} |
| |
|
| int strcnt(const char *s, const char c) { |
#if ! defined(SELF) && ! defined(SELFTEST) |
| |
|
| |
/* |
| |
* Functions strtolower() and strtoupper(), used by function below, |
| |
* are defined in strctype.c so when we are building self-testing |
| |
* binary, skip this section. |
| |
*/ |
| |
|
| |
char *stristr(const char *s1, const char *s2) { |
| |
char *a_s1, *a_s2; |
| |
register char *ret; |
| |
|
| |
a_s1 = strdup(s1); |
| |
a_s2 = strdup(s2); |
| |
|
| |
if (a_s1 == NULL || a_s2 == NULL) { |
| |
free(a_s1); |
| |
free(a_s2); |
| |
return NULL; |
| |
} |
| |
|
| |
if ((ret = strstr(strtolower(a_s1), strtoupper(a_s2))) != NULL) |
| |
ret = (char *) s1 + (ret - a_s1); |
| |
|
| |
free(a_s1); |
| |
free(a_s2); |
| |
|
| |
return ret; |
| |
} |
| |
|
| |
#endif |
| |
|
| |
int strcnt(const char *str, const char c) { |
| register int i = 0; |
register int i = 0; |
| |
|
| if (s != NULL) |
if (str != NULL) |
| while (*s != '\0') |
while (*str != '\0') |
| if (*s++ == c) |
if (*str++ == c) |
| i++; |
i++; |
| |
|
| return i; |
return i; |
| } |
} |
| |
|
| |
int strcnt_str(const char *str, const char *substr) { |
| |
register char *s; |
| |
register int count; |
| |
|
| |
for (count = 0; ; count++) { |
| |
if ((s = strstr(str, substr)) == NULL) |
| |
break; |
| |
else |
| |
str += (s - str) + 1; |
| |
} |
| |
|
| |
return count; |
| |
} |
| |
|
| |
int strcnt_sepstr(const char *str, const char *substr) { |
| |
register char *s; |
| |
register int count, substr_size; |
| |
|
| |
substr_size = strlen(substr); |
| |
|
| |
for (count = 0; ; count++) { |
| |
if ((s = strstr(str, substr)) == NULL) |
| |
break; |
| |
else |
| |
str += (s - str) + substr_size; |
| |
} |
| |
|
| |
return count; |
| |
} |
| |
|
| char *strdel(char *s) { |
char *strdel(char *s) { |
| register int i; |
register int i; |
| |
|
| Line 58 int strrcmp(const char *s1, const char * |
|
| Line 120 int strrcmp(const char *s1, const char * |
|
| return strlen(s1) - strlen(s2); |
return strlen(s1) - strlen(s2); |
| } |
} |
| |
|
| #if ! defined(SELF) && ! defined(SELFTEST) |
#if defined(SELF) || defined(SELFTEST) || defined(SELF_STRPLUS) |
| |
|
| /* |
#include <stdio.h> |
| * Functions strtolower() and strtoupper(), used by function below, |
|
| * are defined in strctype.c so when we are building self-testing |
|
| * binary, skip this section. |
|
| */ |
|
| |
|
| char *stristr(const char *s1, const char *s2) { |
|
| char *a_s1, *a_s2; |
|
| register char *ret; |
|
| |
|
| a_s1 = strdup(s1); |
|
| a_s2 = strdup(s2); |
|
| |
|
| if (a_s1 == NULL || a_s2 == NULL) { |
|
| free(a_s1); |
|
| free(a_s2); |
|
| return NULL; |
|
| } |
|
| |
|
| if ((ret = strstr(strtolower(a_s1), strtoupper(a_s2))) != NULL) |
|
| ret = (char *) s1 + (ret - a_s1); |
|
| |
|
| free(a_s1); |
|
| free(a_s2); |
|
| |
|
| return ret; |
|
| } |
|
| |
|
| #endif |
|
| |
|
| #if defined(SELF) || defined(SELFTEST) |
|
| |
|
| #define TESTSTR1 "___AAA_BBBB__C_DaDaD____" |
#define TESTSTR1 "___AAA_BBBB__C_DaDaD____" |
| #define TESTSEP1 '_' |
#define TESTSEP1 '_' |
| |
#define TESTSEP2 "BB" |
| |
|
| int main(void) { |
int main(void) { |
| |
|
| printf("strcnt(\"%s\", '%c') = %d\n", |
printf("strcnt(\"%s\", '%c') = %d\n", |
| TESTSTR1, TESTSEP1, strcnt(TESTSTR1, TESTSEP1)); |
TESTSTR1, TESTSEP1, |
| |
strcnt(TESTSTR1, TESTSEP1)); |
| |
|
| |
printf("strcnt_str(\"%s\", \"%s\") = %d\n", |
| |
TESTSTR1, TESTSEP2, |
| |
strcnt_str(TESTSTR1, TESTSEP2)); |
| |
|
| |
printf("strcnt_sepstr(\"%s\", \"%s\") = %d\n", |
| |
TESTSTR1, TESTSEP2, |
| |
strcnt_sepstr(TESTSTR1, TESTSEP2)); |
| |
|
| return 0; |
return 0; |
| } |
} |