字符串函数测试

#include "stdafx.h" #include <stdio.h> #include <string.h> #include <stdlib.h> char *my_strchr(char *szDst, char cSrc) { if ('/0' == *szDst || NULL == cSrc) { return NULL; } while(*szDst && (*szDst != cSrc)) { szDst++; } if (*szDst == cSrc) { return szDst; } return NULL; } int test_my_strchr(void) { char *szTest = "abcabcabc"; char *rst; rst = my_strchr(szTest,'b'); printf("test_my_strchr/n"); printf("%s/n", rst); return 0; } char *my_strstr(const char *stDst, const char *stSrc) { char *s1,*s2,*ch = (char *)stDst; if (stDst == stSrc) { return (char *)stDst; } if (NULL == stSrc || NULL == stDst) { return NULL; } if ('/0' == *stSrc) { return (char *)stDst; } while(*ch) { s1 = ch; s2 = (char *)stSrc; while(*s1 && *s2 && *s1 == *s2) { s1++; s2++; } if ('/0' == *s2)//若s2长度不符则直接结束 { return ch; } if ('/0' == *s1) { return NULL; } ch++; } return NULL; } int strstr_main(void) { char *str1 = "fadfadf"; char *str2 = "fa"; char *ptr1,*ptr2; ptr1 = my_strstr(str1,str2); ptr2 = strstr(str1,str2); printf("strstr_main/n"); printf("The substring is : %s/n", ptr1); printf("The substring is : %s/n", ptr2); return 0; } char *my_strcpy(char *strDst, const char *strSrc) { if (NULL == strSrc) { return (char *)strSrc; } if ('/0' == *strSrc) { return (char *)strSrc; } char *ch = (char *)strSrc; while(*strDst ++ = *strSrc++); return (char*)ch; } int strcpy_main(void) { char string[10]; char *str1 = "abc"; char *ch; ch = my_strcpy(string, str1); printf("strcpy_main/n"); printf("%s/n", string); printf("%s/n", str1); printf("%s/n", ch); return 0; } int my_strcmp(const char *szDst, const char *cSrc) { int i = 0; if (NULL == szDst || NULL == cSrc) { return NULL; } while(*szDst && *cSrc && (*szDst == *cSrc)) { szDst++; cSrc++; } i = *szDst - *cSrc; if (i < 0) { i = -1; } if (i == 0) { i = 0; } if (i > 0) { i = 1; } return i; } int test_my_strcmp(void) { int szRet; char *szText; szText = "abc"; szRet = my_strcmp(szText, "abcd"); if(szRet < 0) printf("test_my_strcmp sucess/n"); return 0; } int my_stricmp(const char *st1, const char *st2) { int i = 0; char *str1 = (char *)st1, *str2 = (char *)st2; if (NULL == str1 || NULL == str2) { return NULL; } while(*str1 && *str2 && (*str1 == *str2) || (*str1 == *str2+32) || (*str2 == *str1+32)) { str1++; str2++; } i = *str1 - *str2; if (i < 0) { i = -1; } if (i == 0) { i = 0; } if (i > 0) { i = 1; } return i; } int test_my_stricmp(void) { int szRet; char *szText; szText = "abc"; szRet = my_stricmp(szText, "ab"); return 0; } size_t my_strlen(const char *szSrc) { int i = 0; if (NULL == szSrc) { return NULL; } while(*(szSrc + i)) { i++; } return i; } int test_my_strlen(void) { char *u = ""; int y = my_strlen(u); int z = strlen(u); printf("%d/n", y); printf("%d/n", z); return 0; } char *my_strcat(char *szDst, char *cSrc)//返回的是szDst的首地址 { char *ch = szDst; while(*szDst) { szDst++; } while(*szDst++ = *cSrc++);//是while((*szDst++ = *cSrc++) != '/0'){NULL};缩写 // return szDst; return ch; } int test_my_strcat(void) { char *szRet; char szText[20]; char *szTest; szTest = "af"; strcpy(szText,"AB"); szRet = my_strcat(szText,szTest); printf("test_my_strcat/n"); printf("%s/n", szRet); return 0; } void * my_memset(void *szDst, int cSrc, size_t count) { char *str = (char *)szDst; while (count > 0) { count--; (*str++) = cSrc; } return szDst; } int test_my_memset(void) { void *szRet; char buf[] = "abcdef"; // char acTmp[10]; szRet = my_memset(buf,'1', 2); printf("test_my_memset/n"); printf("buf:%s/n",szRet); return 0; } void *my_memcpy(void *szDst, const void *stSrc, size_t count) { char *str1 = (char *)szDst; char *str2 = (char *)stSrc; while(count > 0) { count--; (*str1++) = (*str2++); } return szDst; } int test_my_memcpy(void) { void *szRet; char buf[] = "**********"; char *szText; szText = "??????????"; szRet = my_memcpy(buf,szText,10); printf("test_my_memcpy/n"); printf("buf:%s/n",szRet); return 0; } void main(void) { test_my_strchr(); strstr_main(); strcpy_main(); test_my_strcmp(); test_my_stricmp(); test_my_strlen(); test_my_strcat(); test_my_memset(); test_my_memcpy(); //getchar(); }

你可能感兴趣的:(字符串函数测试)