采用顺序结构存储串,编写一个index(s1,s2),用于s2是否是s1的子串。若是,返回其在主串中的位置;否则返回-1,完整代码
时间: 2024-02-17 08:03:22 浏览: 29
以下是采用顺序结构存储串的index函数实现代码:
```c
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
typedef struct{
char ch[MAXLEN];
int length;
}SqString;
int index(SqString s1, SqString s2){
int i,j;
for(i=0; i<=s1.length-s2.length; i++){
for(j=0; j<s2.length; j++){
if(s1.ch[i+j]!=s2.ch[j])
break;
}
if(j==s2.length)
return i+1;
}
return -1;
}
int main(){
SqString s1={"Hello, World!", 13};
SqString s2={"World", 5};
printf("%d\n", index(s1, s2)); // output: 7
return 0;
}
```
代码思路:遍历主串s1,对于每一个字符,判断其后是否与子串s2相同,若是,则返回该子串在主串中的位置;否则继续遍历。若遍历完主串s1后仍未找到子串s2,则返回-1。
相关问题
采用顺序结构存储串,编写一个函数index(s1,s2),用于s2是否是s1的子串。若是,返回其在主串的位置;否则返回-1.用c语言编写完整代码
下面是采用顺序结构存储串,编写的函数index(s1,s2)的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
int index(char *s1, char *s2) {
int i, j, k;
int len1 = strlen(s1), len2 = strlen(s2);
for (i = 0; i < len1 - len2 + 1; i++) {
for (j = i, k = 0; s2[k] != '\0' && s1[j] == s2[k]; j++, k++);
if (k == len2) return i;
}
return -1;
}
int main() {
char s1[MAXLEN], s2[MAXLEN];
printf("请输入主串s1:");
scanf("%s", s1);
printf("请输入子串s2:");
scanf("%s", s2);
int pos = index(s1, s2);
if (pos != -1) {
printf("子串在主串中的位置为:%d\n", pos);
} else {
printf("子串不是主串的子串!\n");
}
return 0;
}
```
该函数的思路是:枚举主串中每一个可能的子串起点,然后逐个字符比较主串和子串对应位置上的字符。如果某个位置上的字符不匹配,就跳出当前循环,开始枚举下一个子串起点。如果子串中的所有字符都能和主串中对应的字符匹配,就说明子串是主串的一个子串,返回子串在主串中的起始位置。如果遍历完所有可能的子串起点后都没有找到子串,就返回-1表示子串不是主串的子串。
以上代码可以通过以下方式编译并运行:
```bash
$ gcc -o main main.c
$ ./main
请输入主串s1:hello world
请输入子串s2:world
子串在主串中的位置为:6
```
采用顺序结构存储串,用c语言编写一个函数index(s1,s2),用于s2是否是s1的子串。若是,返回其在主串中的位置;否则返回-1。
好的,下面是一个简单的用C语言实现的index函数,用于判断s2是否是s1的子串,并返回其位置:
```c
int index(char s1[], char s2[]) {
int i, j, k;
int len1 = strlen(s1);
int len2 = strlen(s2);
for (i = 0; i <= len1 - len2; i++) {
k = i;
for (j = 0; j < len2; j++) {
if (s1[k] == s2[j]) {
k++;
} else {
break;
}
}
if (j == len2) {
return i;
}
}
return -1;
}
```
在此函数中,我们使用了两个循环,第一个循环用于遍历s1中所有可能的子串,第二个循环用于判断当前子串是否与s2相同。如果找到了相同的子串,则返回其位置,否则返回-1。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)