C语言字符串长度和字符串复制实现

来源:互联网 发布:串口软件 编辑:程序博客网 时间:2024/06/05 20:41
#include <stdio.h>#include <stdlib.h>#include <string.h>//字符串的长度int mystrlen(const char* str){    if (str == NULL)        return -1;    int len = 0;    while (*str++ != '\0')    {        len++;    }    return len;}//从strSrc复制到strDest//其中strDest长度要大于strSrc的长度char* mystrcpy(char* strDest, const char* strSrc){    if (strDest == NULL || strSrc == NULL)        return NULL;    if (strDest == strSrc)        return strDest;    char* tmp = strDest;    while ((*strDest++ = *strSrc++) != '\0');    return tmp;}int main(){    char* q = "abcdefg";    //strcpy_s(q,sizeof("123456"), "123456");    printf("%d\n", mystrlen(q));    printf("%d\n", strlen(q));    //printf("%s\n", q);    return 0;}
0 0