c语言编写一个程序,输入两个字符串 s1 和 s2 ,判断 s2 是否是 s1 的子串,如果是,则输出 s2 在 s1 中的起始位置。否则输出 0 输入:两个字符串 s1 和 s2 输出:s2 在 s1 中的起始位置 或者 输出 0 例如: 输入:helloworld world 输出:6
时间: 2024-02-25 20:55:37 浏览: 74
好的,以下是 C 语言的程序实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char s1[100], s2[100];
int i, j, flag;
printf("请输入两个字符串:\n");
scanf("%s %s", s1, s2);
for (i = 0; s1[i] != '\0'; i++) {
flag = 1;
for (j = 0; s2[j] != '\0'; j++) {
if (s1[i+j] != s2[j]) {
flag = 0;
break;
}
}
if (flag) {
printf("%d", i+1);
return 0;
}
}
printf("0");
return 0;
}
```
程序的思路是,使用两个循环,外层循环遍历字符串 s1,内层循环遍历字符串 s2,检查 s2 是否是 s1 的子串。如果找到了子串,则输出子串的起始位置,否则输出 0。请注意,字符串的起始位置是从 1 开始计算的。
希望能够帮助到你!
相关问题
c语言编写一个程序,输入两个字符串 s1 和 s2 ,判断 s2 是否是 s1 的子串,如果是,则输出 s2 在 s1 中的起始位置。否则输出 0 输入:两个字符串 s1 和 s2 输出:s2 在 s 1 中的起始位置 或者 输出 0
好的,以下是用C语言实现的程序:
```c
#include <stdio.h>
#include <string.h>
int kmp(const char* s1, const char* s2) {
int n1 = strlen(s1), n2 = strlen(s2);
int nxt[n2];
int i = 0, j = -1;
// 构建 next 数组
nxt[0] = -1;
while (i < n2 - 1) {
if (j == -1 || s2[i] == s2[j]) {
i++, j++;
nxt[i] = (s2[i] == s2[j]) ? nxt[j] : j;
} else {
j = nxt[j];
}
}
// 匹配
i = 0, j = 0;
while (i < n1 && j < n2) {
if (j == -1 || s1[i] == s2[j]) {
i++, j++;
} else {
j = nxt[j];
}
}
if (j == n2) {
return i - n2;
} else {
return 0;
}
}
int main() {
char s1[1000], s2[1000];
int pos;
printf("请输入字符串 s1 和 s2:\n");
scanf("%s%s", s1, s2);
pos = kmp(s1, s2);
if (pos) {
printf("s2 在 s1 中的起始位置是:%d\n", pos);
} else {
printf("s2 不是 s1 的子串。\n");
}
return 0;
}
```
其中,`kmp` 函数实现了 KMP 算法,用于在 `s1` 中匹配 `s2` 是否为其子串。在 `main` 函数中,读入两个字符串并调用 `kmp` 函数进行匹配,输出匹配结果。
用c语言编写一个程序,输入两个字符串 s1 和 s2 ,判断 s2 是否是 s1 的子串,如果是,则输出 s2 在 s1 中的起始位置。否则输出 0
好的,以下是 C 语言代码实现:
```c
#include <stdio.h>
#include <string.h>
int find_substring(char *s1, char *s2) {
/*
判断字符串 s2 是否是 s1 的子串,如果是,则返回 s2 在 s1 中的起始位置,否则返回 0
*/
char *pos = strstr(s1, s2);
if (pos != NULL) {
return pos - s1 + 1;
} else {
return 0;
}
}
int main() {
char s1[100], s2[100];
printf("请输入字符串 s1:");
scanf("%s", s1);
printf("请输入字符串 s2:");
scanf("%s", s2);
int position = find_substring(s1, s2);
if (position != 0) {
printf("%s 是 %s 的子串,起始位置为 %d\n", s2, s1, position);
} else {
printf("%s 不是 %s 的子串\n", s2, s1);
}
return 0;
}
```
使用示例:
```
请输入字符串 s1:hello, world!
请输入字符串 s2:world
world 是 hello, world! 的子串,起始位置为 8
```
希望能够帮助到你!
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)