自己实现strstr函数与strchr函数
生活随笔
收集整理的這篇文章主要介紹了
自己实现strstr函数与strchr函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
const char *my_strstr(const char *str, const char *sub_str) ?
{ ?
? ? for(int i = 0; str[i] != '\0'; i++) ?
? ? { ?
? ? ? ? int tem = i; //tem保留主串中的起始判斷下標位置 ??
? ? ? ? int j = 0; ?
? ? ? ? while(str[tem++] == sub_str[j++]) ?
? ? ? ? { ?
? ? ? ? ? ? if(sub_str[j] == '\0') ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return &str[i]; ?
? ? ? ? ? ? } ?
? ? ? ? } ? ?
? ? } ?
??
? ? return NULL; ?
}?
char * Strchr(char to[], char Ch)
{
? ? int i = 0;
? ? while (to[i] != Ch && to[i] != '\0') ? ? i++;
? ? return to[i] != '\0' ? to + i : NULL;
}
與50位技術專家面對面20年技術見證,附贈技術全景圖
{ ?
? ? for(int i = 0; str[i] != '\0'; i++) ?
? ? { ?
? ? ? ? int tem = i; //tem保留主串中的起始判斷下標位置 ??
? ? ? ? int j = 0; ?
? ? ? ? while(str[tem++] == sub_str[j++]) ?
? ? ? ? { ?
? ? ? ? ? ? if(sub_str[j] == '\0') ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return &str[i]; ?
? ? ? ? ? ? } ?
? ? ? ? } ? ?
? ? } ?
??
? ? return NULL; ?
}?
char * Strchr(char to[], char Ch)
{
? ? int i = 0;
? ? while (to[i] != Ch && to[i] != '\0') ? ? i++;
? ? return to[i] != '\0' ? to + i : NULL;
}
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的自己实现strstr函数与strchr函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速弄懂内存字节对齐
- 下一篇: linux菜鸟入门-1