首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

m串是否在str中存在 成功返回位置//求在本程序上修改,该怎么处理

2012-04-20 
m串是否在str中存在 成功返回位置//求在本程序上修改[codeC/C++]#include stdio.h#include string.hi

m串是否在str中存在 成功返回位置//求在本程序上修改
[code=C/C++]
#include <stdio.h>
#include <string.h>

int fun(char* str,char* m)
{
  int i,j=0,len=0,;
  for(i=0;str[i]!='\0';i++)
  {

  if(str[i]==m[j])
  {
  j++;
  len++;
  if(m[j]=='\0') return len;
  }
  else if(str[i]!=m[j])
  {
  j=0;
  len=0;
  }
  }
}
int main()
{
  printf("pos=%d\n",fun("heeeello","el"));
}

[code]

[解决办法]

C/C++ code
#include <stdio.h>#include <string.h>int fun(char* str,char* m){   int i,len=0;   for(i=0;str[i]!='\0';i++)   {   if(str[i]==m[0])   {   for(len=1;m[len]!='\0'&&str[i+len]!='0';len++)   {   if(m[len]!=str[i+len])break;   }      if(m[len]=='\0') return len;   }   }   return -1;}int main(){   printf("pos=%d\n",fun("heeeello","el"));} 

热点排行