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"); }
为什么要奋斗?因为我有梦想!为了诗和远方!