字符串匹配查找的实现.txt

来自「c语言的一些常见的算法以及思考和改进的文章,写的很不错,花费了很大的精力从网络了」· 文本 代码 · 共 36 行

TXT
36
字号
字符串匹配/查找的实现[原创] 
       该程序查找second字符串在first字符串中的位置,如果不存在,则返回-1。另外,这个程序还有很大的可优化余地,以后有时间慢慢补充。

#i nclude <conio.h>

int AtPos(char *p,char *q)/* to judge whether the second string exists int the first one*/
{
   int i,j;
   for(i=0;p[i];i++)
   {
       for(j=0;q[j]&&q[j]==p[i+j];j++)
               ;
       if(!q[j])
             return i;
   }
   return -1;
}

void main()
{
   char first[80],second[80];
   int pos;
   clrscr();
   printf("\nPlease input the first string....\n");
   gets(first);
   printf("\nPlease input the second string....\n");
   gets(second);
   pos=AtPos(first,second);
   if(pos==-1)
      printf("\nThe second string does not exist in the first one.\n");
   else
      printf("\nFOUND!The Position is %4d\n",pos);

}
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?