C和指针之strcat函数 strchr函数 strcmp函数 strcpy函数 strnchr函数 strstr函数实现
生活随笔
收集整理的這篇文章主要介紹了
C和指针之strcat函数 strchr函数 strcmp函数 strcpy函数 strnchr函数 strstr函数实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 strcat函數實現
#include <stdio.h> //簡單實現strcat函數 char *my_strcat(char *des, const char *src) {if (des == NULL || src == NULL)return des;char *result = des;//把指針移到末尾while (*des)des++;printf("*des is %c\n", *des);while ((*des++ = *src++) != '\0');return result; } int main() {char des[30] = "chenyu";const char *src = "hello";printf("des is %s and my_strcat result is %s\n",des, my_strcat(des, src));return 0; }運行結果:
/** vim my_strcat.c gcc -g my_strcat.c -o strcat ./strcat *des is des is chenyuhello and my_strcat result is chenyuhello **/
2 strchr函數實現
#include <stdio.h> #include <string.h>/** 簡單模擬strchr函數 **/ char *my_strchr(const char *des, int ch) {if (des == NULL)總結
以上是生活随笔為你收集整理的C和指针之strcat函数 strchr函数 strcmp函数 strcpy函数 strnchr函数 strstr函数实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C和指针之memmove函数 memcp
- 下一篇: DNS的理解