C language-simulate database functions strcpy, strcat, strcmp
1. Database strcpy function, copy a string to another string
#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));}
Ii. Database strcat function, append a string to another string
#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));}
3. strcmp function, returns 0 if two strings str1 and str2 are equal; str1> str2 returns 1; else returns-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));}