c判断一个串是否为另一个串的子串
时间: 2024-06-13 17:07:42 浏览: 19
以下是C语言中判断一个串是否为另一个串的子串的方法:
```c
#include <stdio.h>
#include <string.h>
int isSubstring(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int i, j;
for(i = 0; i <= len1 - len2; i++) {
for(j = 0; j < len2; j++) {
if(str1[i+j] != str2[j])
break;
}
if(j == len2)
return i;
}
return -1;
}
int main() {
char str1[] = "hello world";
char str2[] = "world";
int result = isSubstring(str1, str2);
if(result == -1)
printf("%s is not a substring of %s\n", str2, str1);
else
printf("%s is a substring of %s, starting at position %d\n", str2, str1, result);
return 0;
}
```
输出结果为:
```
world is a substring of hello world, starting at position 6
```