字符串操作函数源代码实现

来源:互联网 发布:购买域名后怎么建网站 编辑:程序博客网 时间:2024/05/14 17:48
//1字符串拷贝函数char* My_strcpy( char* strDest,const char* strSrc){if(NULL == strDest || NULL == strSrc){return NULL;}char* strTemp = strDest;while( (*strDest++ = *strSrc++) != '\0' );printf("%s",strDest);return strTemp;}//2字符串长度int My_strlen(const char* strTest){if ( NULL == strTest){return 0;}int i = 0;while( *strTest++ != '\0' )i++;return i;}//3查找第一个字符char* My_strchr(const char* strTest,int c){if ( NULL == strTest){return NULL;}//char* strTemp = strTest;while( *strTest++ != c)if (*strTest){return (char*)strTest;}return NULL;}//4字符串比较函数int My_strcmp(const char* str1,const char* str2){if ( NULL == str1 || NULL == str2){return -10;}while( *str1 && *str2 && (*str1 == *str2) ){str1++;str2++;}return(*str1 - *str2);}//5字符串连接函数char* My_strcat( char *strDestination, const char *strSource){if ( NULL == strDestination || NULL == strSource){return NULL;}char* strTemp = strDestination;while( *strDestination != '\0'){strDestination++;}while( ((*strDestination++ = *strSource++)) != '\0' );return strTemp;}//6字符串拷贝到新的内存char* My_strdup(const char* strDestination){if ( NULL == strDestination){return NULL;}int nLength = 0;char* strTest = (char*)strDestination;while( *strTest++ != '\0'){nLength++;}char* strTemp = (char*)malloc(nLength+1);char* strReturn = strTemp;while( (*strTemp = *strDestination) != '\0'){strTemp++;strDestination++;}return strReturn;}//7查找字符串char* My_strstr(const char* strSrc,const char* str){//assert(strSrc != NULL && str != NULL);const char *s = strSrc;const char *t = str;for (; *t != '\0'; ++ strSrc){printf("%s\n",strSrc);for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t)NULL;if (*t == '\0')return (char *) strSrc;}return NULL;}

原创粉丝点击