模擬實現與字串相關的函式(較全)

2021-09-12 01:58:40 字數 1645 閱讀 9485

//模擬實現strcpy,會將\0一起拷貝

char* my_strcpy(char* dest,char* str)

*dest = *str;

return start;

}int main()

//模擬實現strcat,追加字串會帶上\0

char* my_strcat(char* dest, char* str)

int main()

//模擬實現strcmp,比較對應字元的ascii碼值大小

//不是比較字串長度

int my_strcmp(const char* str, const char* src)

/*if (*str > *src)

return 1;

else

return -1;*/

return *str - *src;

}int main()

//模擬實現strncpy,拷貝要求長度的字串,不帶\0,

//若是字元長度不夠要求長度,用0補充

char* my_strncpy(char* dest, char* str, int count)

while (count)

return start;

}int main()

//模擬實現strncat,連線之後會帶上\0

//若是字串不夠要求的長度,直接忽略,不用添0

char* my_strncat(char* dest, char* str,int count)

*dest = *str;

return start;

}int main()

//模擬實現strncmp

//減法運算判斷

int my_strncmp1(const char *src, const char *dest, size_t count)

if (ret < 0)

else if (ret > 0)

return ret;

}//比較運算判斷

int my_strncmp2(const char* dest, const char* src, size_t count)

if (*(unsigned char*)dest > *(unsigned char*)src)

return 1;

else if (*(unsigned char*)dest < *(unsigned char*)src)

return -1;

else

return 0;

}int main()

//模擬實現strstr,若是找到字串,返回從該字串開始的後面所有字串

//若是未找到字串則返回空串(null)

const char* my_strstr(const char* dest, const char* str)

return(null);

}int main()

模擬實現字串庫函式

1.strcat 1 函式功能 實現兩個字串的連線 2 思想 首先遍歷目標字串,找到 0 的位址,然後將資源字串通過指標一次一次的拼接在目標字串後面,直到指標走到資源字串的 0 3 char mystrcat char strdestination,const char strsource whil...

字串模擬實現

1.三種方式模擬實現strlen函式。方法1 用計數器模擬實現 define crt secure no warnings include include include include int mystrlen char str return ret int main printf 請輸入字串 n...

字元函式和字串函式的模擬實現

strlen 算字串的長度 size t strlen const char str 1.模擬實現strlen include include intmy strlen const char p return count int main strcpy 字串拷貝 char strcpy char d...