C语言匹配字符串

#include <stdio.h>
#include <stdlib.h>int MyStr1(char * str1, char * str2)
{
    int num = 1;
    while (*str1 != '\0')
    {
        if (*str1 != *str2)
        {
            str1++;
            num++;
            continue;
        }
        if (memcmp(str1, str2, 3) == 0)
        {
            return num;
        }
        else
        {
            str1++;
       
num++;
      continue;
      }

}

  return -1;
}

 

int MyStr(char * str1, char * str2)
{
    int num = 1;
    while (*str1 != '\0')
    {
        if (*str1 != *str2)
        {
            str1++;
            num++;
            continue;
        }
        char * temp1 = str1;
        char * temp2 = str2;
        while (*temp2 != '\0')
        {
            if (*temp1 != *temp2)
            {
                str1++;
                num++;
                break;
            }
            temp1++;
            temp2++;
        }
        if (*temp2 == '\0')
        {
            return num;
        }
    }

    return -1;
}


void test01()
{
    char * str = "abcdefg";

    int ret = MyStr(str, "fg");
    if (ret == -1)
    {
        printf("No\n");
    }
    else
    {
        printf("Yes  %d:\n", ret);
    }

}

void main()
{
    test01();


    system("pause");
}

 

posted @ 2018-07-26 09:08  LifeOverflow  阅读(5154)  评论(0编辑  收藏  举报