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");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM