• 嵌入式交流群:705573984 资源分享群:708912078

查找子串在字符串中第一个出现的地址

嵌入式 admin 4年前 (2019-03-07) 1755次浏览 0个评论

自己编的,可能算法不是那么完美但是基本功能可以实现

#include<stdio.h>
int my_strlen(char *ps1,char *ps2); //声明函数
int my_strlen(char *ps1,char *ps2)
{
    char str1[100];
    char str2[100];
     int i=0,a=0,b=0,c=0,d=0,e=0;

     for(i=0;*ps1;i++)
    {
     str1[i]=*ps1++; //ps1指向的值赋给str1
     a++;                  //字符串的长度
     }
     ps1-=a; //指针回到原来的位置
     for(i=0;*ps2;i++)
    {
    str2[i]=*ps2++; //同上
    b++;
    }
    ps2-=b; //同上
   for(i=0;str1[i];i++)
   {
   if(str1[i]==str2[0])  //判断str1的i位是否等于str2的0位
  {
   d=i;

   for(c=1;str1[c];c++)
   {
      if(str1[++d]!=str2[c])  //判断后面的字符串是否相等
       break;         //不相等break ,相等的话c的值会等于str2字符串的长度
    }
       if(c==b)
   {
   //printf("%p\n",&str1[i]);
   printf("重复字符串首地址:%p\n",(ps1+i));
   e=1;
   break;
   }
   }
   }
   if(e==0) printf("没有重复的字符串\n");
   return 0;
    }

int main()
{
    char *ps1="helloword",*ps2="low";    //程序提前设置好的,也可以加上用户自己输入字符串的功能
   printf("helloword的首地址:%p\n",ps1);
   my_strlen(ps1,ps2);
    return 0;
}


喜欢 (2)

您必须 登录 才能发表评论!