-->
当前位置:首页 > 题库 > 正文内容

函数题:字符串匹配 - C/C++ 数组及字符串

Luz3年前 (2022-09-06)题库582
函数findSubStr()用于从以0结尾的字符数组s中查找其子串s1的位置,如果找到,返回起始下标,否则返回-1。

int findSubStr(char s[], char s1[]);


举例:字符串'tri'在字符串'string'中的起始下标为1;在字符串'tom&jerry'中找不到子串'jack',函数应返回-1。

请定义上述findSubStr()函数,使得下述程序可以运行并产生正确的执行结果。


### 函数接口定义:
c++
int findSubStr(char s[], char s1[]);


### 裁判测试程序样例:
c++
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

//findSubStr()函数定义处

int main()
{
char s1[1000] = "";
char s2[1000] = "";

scanf("%s",s1);
scanf("%s",s2);
int r = findSubStr(s1,s2);

if (r<0)
printf("Not found.");
else
printf("Found \'%s\' at position %d of \'%s\'",s1,r,s2);
return 0;
}


### 输入样例:
in
tomHat
om


### 输出样例:
out
Found 'tomHat' at position 1 of 'om'


### 感觉不会?  那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)







答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。