回到在两字符串中第一个相同的子串(大于1个字符),并将子串输出
返回在两字符串中第一个相同的子串(大于1个字符),并将子串输出
1,返回在两字符串中第一个相同的子串(大于1个字符),并将子串输出
比如:
输入
adbAFEHHFS
acwfagAFEf
输出为 AFE
2,从键盘输入一个字符串,然后再输入一个字符串,以第二个字符串整体作为分割条件把第一个字符串进行分割,然后输出分割之后的单词;
输入
abc@#def@ghi#@jkl
@#
输出为
abc
def@ghi
jkl
------解决方案--------------------
神linus写的代码
------解决方案--------------------
memcmp
高效的:
用户的:
这就是神写的代码
1,返回在两字符串中第一个相同的子串(大于1个字符),并将子串输出
比如:
输入
adbAFEHHFS
acwfagAFEf
输出为 AFE
2,从键盘输入一个字符串,然后再输入一个字符串,以第二个字符串整体作为分割条件把第一个字符串进行分割,然后输出分割之后的单词;
输入
abc@#def@ghi#@jkl
@#
输出为
abc
def@ghi
jkl
------解决方案--------------------
神linus写的代码
/**
129 * strstr - Find the first substring in a %NUL terminated string
130 * @s1: The string to be searched
131 * @s2: The string to search for
132 */
133 char *strstr(const char *s1, const char *s2)
134 {
135 size_t l1, l2;
136
137 l2 = strlen(s2);
138 if (!l2)
139 return (char *)s1;
140 l1 = strlen(s1);
141 while (l1 >= l2) {
142 l1--;
143 if (!memcmp(s1, s2, l2))
144 return (char *)s1;
145 s1++;
146 }
147 return NULL;
148 }
------解决方案--------------------
memcmp
高效的:
3 int memcmp(const void *s1, const void *s2, size_t len)
4 {
5 u8 diff;
6 asm("repe; cmpsb; setnz %0"
7 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
8 return diff;
9 }
用户的:
651 int memcmp(const void *cs, const void *ct, size_t count)
652 {
653 const unsigned char *su1, *su2;
654 int res = 0;
655
656 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
657 if ((res = *su1 - *su2) != 0)
658 break;
659 return res;
660 }
这就是神写的代码