我正在尝试编写函数(findString)来查找字符串是否存在于另一个字符串中。对于我的函数,第一个参数是搜索到的字符串,第二个是试图找到的字符串。使用函数在字符串中查找字符串
如果找到字符串,则返回源字符串的位置。
我写的代码如下:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int findstring(char source[], char lookup[]){
int lensource, lenlookup;
lensource= strlen(source);
lenlookup = strlen(lookup);
int i;int j;
for(i=0; i>=lensource; ++i)
for (j=0; j>=lenlookup; ++j)
if (source[i]==lookup[j])
return i;
}
int main(){
findstring("Heyman","ey");
}
如果功能正常工作,那么该指数2应返回。
但是,当我运行它时,没有返回。我想问题是,我的方法与for循环或if语句有什么问题。
我这样做是不使用的strstr
使用'strstr' .... –
我的坏我应该包括,我试图避免这种方式! –
你会如何知道是否返回任何内容?你不检查'findstring()'的返回码... – DaV