帮忙做一下c++编程

来源:百度知道 编辑:UC知道 时间:2024/05/15 06:21:13
编写函数int index(char *s,char *t)返回字符串t在字符串s中出现的最左边的位置。如果在s中没有与t匹配的子串就返回-1

strstr函数
简单易用。
使用方法:
strstr
原型:extern char *strstr(char *haystack, char *needle);

用法:#include <string.h>

功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。

说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。

举例:

// strstr.c

#include <syslib.h>
#include <string.h>

main()
{
char *s="Golden Global View";
char *l="lob";
char *p;

clrscr();

p=strstr(s,l);
if(p)
printf("%s",p);
else
printf("Not Found!");

getchar();
return 0;
}
zz from baike

最简单的暴力法

int i, j;
if(s == 0 || t == 0 ) return -1;

for (i = 0; i < strlen(s) - strlen(t); ++i) {
if(s[i] == t[0]) {