- #include <stdio.h>
-
#include <string.h>
-
-
int findSubString(const char *s, int slen, const char *d);
-
int parseString(char *s, int slen, const char *d);
-
-
int
-
main(void)
-
{
-
char s[] = "ABBCD";
-
char d[] = "DA";
-
int len = strlen(s);
-
-
printf("s = %s, d = %s\n", s, d);
-
printf("Res = %d\n", findSubString(s, len, d));
-
printf("s = %s, d = %s\n", s, d);
-
printf("Res = %d\n", parseString(s, len, d));
-
printf("s = %s, d = %s\n", s, d);
-
-
return (0);
-
}
- //双循环,费时间
-
int
-
parseString(char *s, int len, const char *d)
-
{
-
int i = 0;
-
int j = 0;
-
-
if (strstr(s, d)) {
-
return (0);
-
}
-
-
for (i = 0; i < len; i++) {
-
char ctmp = *s;
-
for (j = 1; j < len; j++) {
-
*(s+j-1) = *(s+j);
-
}
-
*(s+j-1) = ctmp;
-
-
if (strstr(s, d)) {
-
return (0);
-
} else {
-
return (-1);
-
}
-
}
-
-
return (-1);
-
}
- //先分析后处理,空间换时间
-
int
-
findSubString(const char *s, int slen, const char *d)
-
{
-
char ss[slen*2];
-
-
strcpy(ss,s);
-
strcpy(ss+slen, s);
-
-
if (strstr(ss,d)) {
-
return (0);
-
} else {
-
return (-1);
-
}
- }