#include<stdio.h> #include<assert.h> char* my_srtcpy( char *srt, char*src) { assert(srt); assert(src); char *tmp = src; while (*src++ = *srt++) { ; } return tmp; } int main(void) { char* arr = "abcdef"; char arr1[10]; printf("%s\n",my_srtcpy(arr, arr1)); }
二、庫strcat函數,把一個字符串追加到另一個字符串中
#include<stdio.h> #include<assert.h> char* my_srtcat(char* srt, const char* src) { assert(srt!=NULL); assert(src!=NULL); char *ret = srt; while (*srt) { srt++; } while (*srt++ = *src++) { ; } return ret; } int main(void) { char arr[15] = "abcdef"; char* arr1 = "cd"; printf("%s\n", my_srtcat(arr, arr1)); }
三、strcmp函數,比較兩個字符串str1,str2,相等返回0;str1>str2返回1;else 返回-1
#include<stdio.h> #include<assert.h> int my_srtcmp(const char*srt1, const char* srt2) { assert(srt1); assert(srt2); while (*srt1 == *srt2) { if (*srt1 == '\0') { return 0; } srt1++; srt2++; } if (srt1 > srt2) { return 1; } else { return -1; } } int main(void) { char *arr = "abcd"; char *arr1 = "abc"; printf("%d\n", my_srtcmp(arr, arr1)); }