#ifndef _MYSTR_H #define _MYSTR_H #include <stdio.h> #include <stdlib.h> /*复制*/ char *mystrcpy(char *, const char *); // [destin, source ] /*复制前n个*/ char *mystrncpy(char *, const int, const char *); // [distin, num, source ] /*求字符串串长度*/ int mystrlen(const char *); // [str ] /*字符在字符串中第一次出现的index*/ int myindexof(const char *, const char *); // [str, chr ] /*字符串在字符串中第一次出现的index*/ int myindexofstr(const char *, const char *); // [str, substr ] /*拼接两个字符串*/ char *mystrcat(char *, const char *); // [distin, source ] /*将后字符串的前n个拼接到前字符串末尾*/ char *mystrncat(char *, const int, const char *); // [distin, n, source ] /*字符在字符串中最后一次出现的index*/ int mylastindexof(const char *, const char *); // [str, chr ] /*反转字符串*/ char *mystrrev(char *); // [str ] /*字符串在字符串中最后一次出现的index*/ int mylastindexofstr(const char *, const char *); // [str, substr ] /*获得字符串从index开始到末尾的子串*/ char *mysubstring(char *, const int, const char *); // [tosubstr, begin_index, str ] /*获得字符串从f_index开始到t_index的子串*/ char *myftsubstring(char *, const int, const int, const char *); // [tosubstr, begin_index, end_index, str] /*去除字符串头和串尾的空格(可处理多个)*/ char *mytrimstr(char *); // [str ] /*字符串比较(对应的字符ASCII值的比较)*/ int mystrcmp(const char *, const char *); // [str1, str2 ] /*字符串的所有大写字符变小写*/ char *mytolowerstr(char *); // [str ] /*字符串的所有小写字符变大写*/ char *mytoupperstr(char *); // [str ] /*从字符串中获得指定index的字符*/ char mygetchrfromstr(const int, const char *); // [index, str ] /*以指定字符切割字符串*/ int mystrsplit(char **, char *, const char); // [distin, source, char lmt_chr ] /*将字符串中全部字符设置为指定字符*/ char *mystrset(char *, const char); // [str, set_chr ] /*将字符串中前n个字符设置为指定字符*/ char *mystrnset(char *, const int, const char); // [str, num, set_chr ] /*忽略大小写进行字符串比较*/ int mychricmp(const char, const char); // [chr1, chr2 ] /*忽略大小写进行字符串前n个字符的比较*/ int mystrncmpi(const char *, const int, const char *); // [str1, num, str2 ] /*修改字符串中全部指定的字符为新的字符*/ char *mystrmodchr(char *, const char, const char); // [str, old_chr, new_chr ] /*修改字符串中全部指定的子字符串为新的字符串*/ char *mystrmodstr(char *, const char *, const char *); // [str, old_str, new_str ] /*复制字符串到安全的位置并返回指向它内存的指针*/ char *mystrdup(const char *); // [source ] /*在字符串的指定index处插入字符*/ char *mystrinsertchr(char *, const int, const char); // [str, index, chr ] /*在字符串的指定index处插入字符串*/ char *mystrinsertstr(char *, const int, const char *); // [str, index, insert_str ] /*数字字符串转int类型整数*/ int mystrtoint(const char *); // [int_str ] /*数字字符串转double类型浮点数*/ double mystrtodbl(const char *); // [dbl_str ] ///////////////////////////// void test_mystrcpy(); void test_mystrncpy(); void test_mystrlen(); void test_myindexof(); void test_myindexofstr(); void test_mystrcat(); void test_mystrncat(); void test_mylastindexof(); void test_mystrrev(); void test_mylastindexofstr(); void test_mysubstring(); void test_myftsubstring(); void test_mytrimstr(); void test_mystrcmp(); void test_mytolowerstr(); void test_mytoupperstr(); void test_mygetchrfromstr(); void test_mystrsplit(); void test_mystrset(); void test_mystrnset(); void test_mychricmp(); void test_mystrncmpi(); void test_mystrmodchr(); void test_mystrmodstr(); void test_mystrdup(); void test_mystrinsertchr(); void test_mystrinsertstr(); void test_mystrtoint(); void test_mystrtodbl(); #endif /* _MYSTR_H */
//返回值:成功正 失败NULL char *mystrcpy(char *destin, const char *source){ if (!destin || !source){ return NULL; } char *pd = destin; const char *ps = source; while ((*pd++ = *ps++)) ; return destin; }
//返回值:成功正 失败NULL char *mystrncpy(char *destin, const int num, const char *source){ if (!destin || !source){ return NULL; } char *pd = destin; const char *ps = source; int i = 0; while ((i++ < num) && (*pd++ = *ps++)) ; if (--i == num){ return destin; } else{ for(++i; i > -1; *pd-- = ‘\0‘, --i) ; return NULL; } }
int mystrlen(const char *str){ if (!str){ return -1; } const char *pstr = str; while(*pstr++) ; return (--pstr - str); }
int myindexof(const char *str, const char *chr){ if (!str || !chr ){ return -1; } const char *pstr = str; const char *pchr = chr; char tmpc = ‘\0‘; while((tmpc = *pstr++) != *pchr && tmpc) ; if (!tmpc){ return -1; } else{ return (--pstr - str); } }
int myindexofstr(const char *str, const char *substr){ if (!str || !substr){ return -1; } const char *pstr = str; const char *psubstr = substr; int index = 0; while (*pstr){ if (*psubstr == *pstr){ ++pstr; if (*(++psubstr) == ‘\0‘){ return index; } } else{ if (psubstr == substr){ ++pstr; ++index; } else{ index += (psubstr - substr); psubstr = substr; } } } return -1; }
char *mystrcat(char *destin, const char *source){ if (!destin || !source){ return NULL; } char *pd = destin; const char *ps = source; while(*pd++); for(--pd; (*pd++ = *ps++);) ; return destin; }
char *mystrncat(char *destin, const int n, const char *source){ if(!destin || !source || n > mystrlen(source) || n < 0){ return NULL; } char *pd = destin; const char *ps = source; pd += mystrlen(destin); for(int i = 0; i < n; ++i){ *pd++ = *ps++; } *pd = ‘\0‘; return destin; }
int mylastindexof(const char *str, const char *chr){ if(!str || !chr){ return -1; } const char *pstr = str; const char *pchr = chr; pstr += mystrlen(str); //printf("-----%c\n", *(--pstr)); while(*(--pstr) != *pchr) ; return (pstr - str); }
char *mystrrev(char *str){ if(!str){ return NULL; } int length = mystrlen(str); char *pstr = str; char *pend = str + (length - 1); for(int i = 0; i < (length / 2); ++i){ static char tmp; tmp = *pstr; *pstr++ = *pend; *pend-- = tmp; } return str; }
int mylastindexofstr(const char *str, const char *substr){ if(!str || !substr){ return -1; } const char *pstr = str; const char *psubstr = substr; int strlength = mystrlen(str); int sublength = mystrlen(substr); pstr += (strlength - 1); psubstr += (sublength - 1); int j_sub = 0; int endindex = strlength - 1; for(int i = 0; i < strlength; ++i){ if(*pstr == *psubstr){ --pstr; --psubstr; if(++j_sub == sublength){ return (endindex - sublength + 1); } }else{ if(!j_sub){ --pstr; --endindex; }else{ endindex -= j_sub; psubstr = str + sublength- 1; } } } return -1; }
char *mysubstring(char *tosubstr, const int begin_index, const char *str){ if(!tosubstr || !str || begin_index > mystrlen(str) || begin_index < 0){ return NULL; } char *ptosub = tosubstr; const char *pstr = str; pstr += begin_index; while((*ptosub++ = *pstr++)) ; return tosubstr; }
char *myftsubstring(char *tosubstr, const int begin_index, //左闭右开 const int end_index, const char *str){ if(!tosubstr || !str || begin_index >= end_index || begin_index < 0 || end_index > mystrlen(str)){ return NULL; } char *ptosub = tosubstr; const char *pstr = str; for((pstr += begin_index); pstr < (str + end_index); (*ptosub++ = *pstr++)) ; *ptosub = ‘\0‘; return tosubstr; }
char *mytrimstr(char *str){ //去除前后空格 if(!str){ return NULL; } char *pstr = str; char *p1 = str; char *p2 = str + (mystrlen(str) - 1); while(*p1++ == ‘ ‘) ; while(*p2-- == ‘ ‘) ; for((--p1, ++p2);p1 <= p2; (*pstr++ = *p1++)) ; *pstr = ‘\0‘; return str; }
int mystrcmp(const char *str1, const char *str2){ if(!str1 || !str2){ return -2;//-2表示没法比较 } const char *pstr1 = str1; const char *pstr2 = str2; int flag = 0; while((*pstr1) && (*pstr2)){ if(*pstr1 < *pstr2){ flag = -1; break; }else if(*pstr1 > *pstr2){ flag = 1; break; } ++pstr1; ++pstr2; } if(!(*pstr1) && !(*pstr2)){ flag = 0; }else if(!(*pstr1)){ flag = -1; }else if(!(*pstr2)){ flag = 1; } return flag; }
char *mytolowerstr(char *str){ if(!str){ return NULL; } char *pstr = str; while(*pstr){ if((*pstr >= ‘A‘) && (*pstr <= ‘Z‘)){ *pstr += (‘a‘ - ‘A‘); } ++pstr; } return str; }
char *mytoupperstr(char *str){ if(!str){ return NULL; } char *pstr = str; while(*pstr){ if((*pstr >= ‘a‘) && (*pstr <= ‘z‘)){ *pstr -= (‘a‘ - ‘A‘); } ++pstr; } return str; }
char mygetchrfromstr(const int index, const char *str){ if(!str || index < 0 || index >= mystrlen(str)){ return ‘\0‘; } return *(str + index); }
//将字符串按指定字符切割返回指向各个片段首字符的指针 返回子串个数/失败-1 int mystrsplit(char **destin, char *source, const char lmt_chr){ if(!destin || !source || !lmt_chr){ return -1; } char **pd = destin; char *ps = source; int flag = 0; int sub_num = 0; while(*ps){ if(*ps != lmt_chr){ if(!flag){ *pd++ = ps; ++sub_num; } flag = 1; }else{ *ps = ‘\0‘; flag = 0; } ++ps; } return sub_num; }
char *mystrset(char *str, const char set_chr){ if(!str || !set_chr){ return NULL; } char *pstr = str; for(; *pstr; (*pstr++ = set_chr)) ; return str; }
char *mystrnset(char *str, const int num, const char set_chr){ if(!str || !set_chr || num < 0 || num > mystrlen(str)){ return NULL; } char *pstr = str; for(int i = 0; i < num; (*pstr++ = set_chr), ++i) ; return str; }
int mychricmp(const char chr1, const char chr2){ if(!chr1 || !chr2){ return -2; } int diff = chr1 - chr2; if(diff == 0 || (diff == (‘a‘ - ‘A‘)) || (diff == (‘A‘ - ‘a‘))){ return 0; }else if(diff < 0){ return -1; }else{ return 1; } }
int mystrncmpi(const char *str1, const int num, const char *str2){ if(!str1 || !str2 || num <= 0 || num > mystrlen(str1) || num > mystrlen(str2)){ return -2; } const char *pstr1 = str1; const char *pstr2 = str2; for(int i = 0; i < num; ++i){ int flag = mychricmp(*pstr1++, *pstr2++); if(flag == -1){ return -1; }else if(flag == 1){ return 1; }else if(flag == -2){ return -2; //失败 } } return 0; }
char *mystrmodchr(char *str, const char old_chr, const char new_chr){ if(!str || !old_chr){ //支持换成‘\0‘ return NULL; } char *pstr = str; while(*pstr){ if(*pstr == old_chr){ *pstr = new_chr; } ++pstr; } return str; }
char *mystrmodstr(char *str, const char *old_str,const char *new_str){ if(!str || !old_str || !new_str){ return NULL; } char *pstr = str; int index = 0; while((index = myindexofstr(pstr, old_str)) != -1){ const char *pnew_str = new_str; for(pstr += index; *pnew_str; *pstr++ = *pnew_str++) ; } return str; }
char *mystrdup(const char *source){ //在堆中申请的内存 用时注意 free if(!source){ return NULL; } int str_length = mystrlen(source); char *destin = NULL; if(!(destin = (char *)calloc((str_length + 1), sizeof(char)))){ return NULL; } if(!(mystrcpy(destin, source))){ return NULL; } return destin; }
char *mystrinsertchr(char *str, const int index, const char chr){ int str_length = mystrlen(str); if(!str || index < 0 || index > str_length){ //支持插入‘\0‘ 允许插在串尾 return NULL; } char *pstr = str, *lastp; pstr += str_length; lastp = pstr + 1; for(int i = 0; i <= (str_length - index); (*lastp-- = *pstr--), ++i) ; *(++pstr) = chr; return str; }
char *mystrinsertstr(char *str, const int index, const char *insert_str){ int str_length = mystrlen(str); if(!str || !insert_str || index < 0 || index > str_length){ //允许插在串尾 return NULL; } int insert_str_length = mystrlen(insert_str); char *pstr = str, *lastp; const char *pinsert_str = insert_str; pstr += str_length; lastp = pstr + insert_str_length; for(int i = 0; i <= (str_length - index); (*lastp-- = *pstr--), ++i) ; for(int i = 0; i < insert_str_length; (*(++pstr) = *pinsert_str++), ++i) ; return str; }
int mystrtoint(const char *int_str){ if(!int_str){ fprintf(stderr, "error: input str pointer is null\n"); return 1; } const char *pint_str = int_str; for(; *pint_str == ‘ ‘ || *pint_str == ‘\t‘ || *pint_str == ‘\r‘ || *pint_str == ‘\n‘; ++pint_str) //跳过前面的空格、制表符、换行符 ; int sign = 1; if(*pint_str == ‘-‘ || *pint_str == ‘+‘){ *pint_str == ‘-‘ ? (sign = -1) : (sign = 1); ++pint_str; } int the_intnum = 0; //没做英文字符的处理 那显然不是纯数字的字符串 for(; *pint_str; (the_intnum = (*pint_str - ‘0‘) + 10 * the_intnum), ++pint_str) ; return (sign * the_intnum); }
double mystrtodbl(const char *dbl_str){ if(!dbl_str){ fprintf(stderr, "error: input str pointer is null\n"); return 1; } const char *pdbl_str = dbl_str; for(; *pdbl_str == ‘ ‘ || *pdbl_str == ‘\t‘ || *pdbl_str == ‘\r‘ || *pdbl_str == ‘\n‘; ++pdbl_str) //跳过前面的空格、制表符、换行符 ; double sign = 1.0; if(*pdbl_str == ‘-‘ || *pdbl_str == ‘+‘){ *pdbl_str == ‘-‘ ? (sign = -1.0) : (sign = 1.0); ++pdbl_str; } double num_bef_point = 0.0; double num_aft_point = 0.0; double num_double = 0.0; for(; *pdbl_str != ‘.‘ && *pdbl_str; ++pdbl_str){ num_bef_point = (*pdbl_str - ‘0‘) + 10.0 * num_bef_point; } if(!(*pdbl_str)){; num_double = sign * num_bef_point; }else{ double point_flag = 0.1; for(++pdbl_str; *pdbl_str; ++pdbl_str){ num_aft_point += (*pdbl_str - ‘0‘) * point_flag; point_flag *= 0.1; } num_double = sign * (num_bef_point + num_aft_point); } return num_double; }
void test_mystrcpy(){ printf("\n>>>char *mystrcpy(char *, const char *)\n"); char str[64] = {0}; printf("str:[ %s ]\n", str); printf("soc:[ %s ]\n", "hello world"); mystrcpy(str, "hello world"); printf("str_op:[ %s ]\n", str); }
void test_mystrncpy(){ printf("\n>>>char *mystrncpy(char *, const int, const char *)\n"); char str[64] = {0}; printf("str:[ %s ]\n", str); printf("soc:[ %s ]\n", "hello world"); printf("num:[ %d ]\n", 5); mystrncpy(str,5, "hello world"); printf("str_op:[ %s ]\n", str); }
void test_mystrlen(){ printf("\n>>>int mystrlen(const char *)\n"); char *p = "hello"; printf("str:[ %s ]\n", p); printf("str_op:[ %d ]\n", mystrlen(p)); }
void test_myindexof(){ printf("\n>>>int myindexof(const char *, const char *)\n"); char *p = "aBcaBc"; char c = ‘B‘; printf("str:[ %s ]\n", p); printf("chr:[ %c ]\n", c); printf("str_op:[ %d ]\n", myindexof(p, &c)); }
void test_myindexofstr(){ printf("\n>>>int myindexofstr(const char *, const char *)\n"); char *p1 = "abCDefghCDij"; char *p2 = "CD"; printf("str:[ %s ]\n", p1); printf("str:[ %s ]\n", p2); printf("str_op:[ %d ]\n", myindexofstr(p1, p2)); }
void test_mystrcat(){ printf("\n>>>char *mystrcat(char *, const char *)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘}; char *p = "world"; printf("str1:[ %s ]\n", str); printf("str2:[ %s ]\n", p); mystrcat(str, p); printf("str_op:[ %s ]\n", str); }
void test_mystrncat(){ printf("\n>>>char *mystrncat(char *, const int, const char *)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘}; char *p = "world"; printf("str1:[ %s ]\n", str); printf("str2:[ %s ]\n", p); printf("num:[ %d ]\n", 3); mystrncat(str, 3, p); printf("str_op:[ %s ]\n", str); }
void test_mylastindexof(){ printf("\n>>>int mylastindexof(const char *, const char *)\n"); char *p = "aBcaBc"; char c = ‘B‘; printf("str:[ %s ]\n", p); printf("chr:[ %c ]\n", c); printf("str_op:[ %d ]\n", mylastindexof(p, &c)); }
void test_mystrrev(){ printf("\n>>>char *mystrrev(char *)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘}; printf("str:[ %s ]\n", str); mystrrev(str); printf("str_op:[ %s ]\n", str); }
void test_mylastindexofstr(){ printf("\n>>>int mylastindexofstr(const char *, const char *)\n"); char *p1 = "abCDefghCDij"; char *p2 = "CD"; printf("str1:[ %s ]\n", p1); printf("str2:[ %s ]\n", p2); printf("str_op:[ %d ]\n", myindexofstr(p1, p2)); }
void test_mysubstring(){ printf("\n>>>char *mysubstring(char *, const int, const char *)\n"); char str[] = {0}; char *p = "hello"; printf("str1:[ %s ]\n", str); printf("str2:[ %s ]\n", p); printf("index:[ %d ]\n", 3); mysubstring(str, 3, p); printf("str_op:[ %s ]\n", str); }
void test_myftsubstring(){ printf("\n>>>char *myftsubstring(char *, const int, const int, const char *)\n"); char str[] = {0}; char *p = "hello world"; printf("str1:[ %s ]\n", str); printf("str2:[ %s ]\n", p); printf("from:[ %d ]\n", 3); printf("to:[ %d ]\n", 8); myftsubstring(str, 3, 8, p); printf("str_op:[ %s ]\n", str); }
void test_mytrimstr(){ printf("\n>>>char *mytrimstr(char *)\n"); char str[] = {‘ ‘, ‘ ‘, ‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘ ‘}; printf("str:[ %s ]\n", str); mytrimstr(str); printf("str_op:[ %s ]\n", str); }
void test_mystrcmp(){ printf("\n>>>int mystrcmp(const char *, const char *)\n"); char *p1 = "abcd"; char *p2 = "aBdc"; printf("str1:[ %s ]\n", p1); printf("str2:[ %s ]\n", p2); printf("str_op:[ %d ]\n", mystrcmp(p1, p2)); }
void test_mytolowerstr(){ printf("\n>>>char *mytolowerstr(char *)\n"); char str[64] = {‘a‘, ‘b‘, ‘C‘, ‘D‘, ‘e‘}; printf("str:[ %s ]\n", str); mytolowerstr(str); printf("str_op:[ %s ]\n", str); }
void test_mytoupperstr(){ printf("\n>>>char *mytoupperstr(char *)\n"); char str[64] = {‘a‘, ‘b‘, ‘C‘, ‘D‘, ‘e‘}; printf("str:[ %s ]\n", str); mytoupperstr(str); printf("str_op:[ %s ]\n", str); }
void test_mygetchrfromstr(){ printf("\n>>>char mygetchrfromstr(const int, const char *)\n"); char *p = "hello"; printf("str:[ %s ]\n", p); printf("index:[ %d ]\n", 3); printf("str_op:[ %c ]\n", mygetchrfromstr(3, p)); }
void test_mystrsplit(){ printf("\n>>>int mystrsplit(char **, char *, const char)\n"); char *p[10] = {0}; char **p1 = p; char str[64] = {0}; mystrcpy(str, " ab cd ef GH "); printf("str:[ %s ]\n", str); int num = mystrsplit(p, str, ‘ ‘); //ÒÔ¿Õ¸ñÇиî for(int i = 0; i < num; ++i){ printf("str_op:[ %s ]\n", *p1++); } }
void test_mystrset(){ printf("\n>>>char *mystrset(char *, const char)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘}; printf("str:[ %s ]\n", str); printf("chr:[ %c ]\n", ‘A‘); mystrset(str, ‘A‘); printf("str_op:[ %s ]\n", str); }
void test_mystrnset(){ printf("\n>>>char *mystrnset(char *, const int, const char)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘}; printf("str:[ %s ]\n", str); printf("chr:[ %c ]\n", ‘A‘); printf("num:[ %d ]\n", 3); mystrnset(str, 3, ‘A‘); printf("str_op:[ %s ]\n", str); }
void test_mychricmp(){ printf("\n>>>int mychricmp(const char, const char)\n"); char c1 = ‘a‘; char c2 = ‘A‘; printf("chr1:[ %c ]\n", c1); printf("chr2:[ %c ]\n", c2); printf("str_op:[ %d ]\n", mychricmp(c1, c2)); }
void test_mystrncmpi(){ printf("\n>>>int mystrncmpi(const char *, const int, const char *)\n"); char *p1 = "AAAbc"; char *p2 = "aaaBC"; printf("str1:[ %s ]\n", p1); printf("str2:[ %s ]\n", p2); printf("num:[ %d ]\n", 3); printf("str_op:[ %d ]\n", mystrncmpi(p1, 3, p2)); }
void test_mystrmodchr(){ printf("\n>>>char *mystrmodchr(char *, const char, const char)\n"); char str[64] = {‘a‘, ‘b‘, ‘D‘, ‘c‘, ‘D‘, ‘E‘}; printf("str:[ %s ]\n", str); printf("oldchr:[ %c ]\n", ‘D‘); printf("newchr:[ %c ]\n", ‘W‘); mystrmodchr(str, ‘D‘, ‘W‘); printf("str_op:[ %s ]\n", str); }
void test_mystrmodstr(){ printf("\n>>>char *mystrmodstr(char *, const char *, const char *)\n"); char str[64] = {0}; mystrcpy(str, "abCDEefCDErgfCDE"); printf("str:[ %s ]\n", str); char *p1 = "CDE"; char *p2 = "HHH"; printf("oldstr:[ %s ]\n", p1); printf("newstr:[ %s ]\n", p2); mystrmodstr(str, p1, p2); printf("str_op:[ %s ]\n", str); }
void test_mystrdup(){ printf("\n>>>char *mystrdup(const char *)\n"); char *p1 = "hello", *p2 = NULL; printf("str1:[ %s ]\n", p2); printf("str2:[ %s ]\n", p1); p2 = mystrdup(p1); printf("str_op:[ %s ]\n", p2); free(p2); p2 = NULL; }
void test_mystrinsertchr(){ printf("\n>>>char *mystrinsertchr(char *, const int, const char)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘}; printf("str:[ %s ]\n", str); printf("index:[ %d ]\n", 2); mystrinsertchr(str, 2, ‘W‘); printf("str_op:[ %s ]\n", str); }
void test_mystrinsertstr(){ printf("\n>>>char *mystrinsertstr(char *, const int, const char *)\n"); char str[64] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘}; printf("str:[ %s ]\n", str); printf("index:[ %d ]\n", 2); char *p = "QQQ"; mystrinsertstr(str, 2, p); printf("str_op:[ %s ]\n", str); }
void test_mystrtoint(){ printf("\n>>>int mystrtoint(const char *)\n"); char *p = " +1034"; int num = 0; printf("str:[ %s ]\n", p); printf("num:[ %d ]\n", num); num = mystrtoint(p); printf("str_op:[ %d ]\n", num); }
void test_mystrtodbl(){ printf("\n>>>double mystrtodbl(const char *)\n"); char *p = " +1034.66"; double num = 0; printf("str:[ %s ]\n", p); printf("num:[ %lf ]\n", num); num = mystrtodbl(p); printf("str_op:[ %lf ]\n", num); }
#include "mystring.h" int main() { printf("__________________TEST_MYSTR_BY_XLC___________________\n"); test_mystrcpy(); test_mystrncpy(); test_mystrlen(); test_myindexof(); test_myindexofstr(); test_mystrcat(); test_mystrncat(); test_mylastindexof(); test_mystrrev(); test_mylastindexofstr(); test_mysubstring(); test_myftsubstring(); test_mytrimstr(); test_mystrcmp(); test_mytolowerstr(); test_mytoupperstr(); test_mygetchrfromstr(); test_mystrsplit(); test_mystrset(); test_mystrnset(); test_mychricmp(); test_mystrncmpi(); test_mystrmodchr(); test_mystrmodstr(); test_mystrdup(); test_mystrinsertchr(); test_mystrinsertstr(); test_mystrtoint(); test_mystrtodbl(); printf("\n-------------------------------------------------------\n"); return 0; }
__________________TEST_MYSTR_BY_XLC___________________ >>>char *mystrcpy(char *, const char *) str:[ ] soc:[ hello world ] str_op:[ hello world ] >>>char *mystrncpy(char *, const int, const char *) str:[ ] soc:[ hello world ] num:[ 5 ] str_op:[ hello ] >>>int mystrlen(const char *) str:[ hello ] str_op:[ 5 ] >>>int myindexof(const char *, const char *) str:[ aBcaBc ] chr:[ B ] str_op:[ 1 ] >>>int myindexofstr(const char *, const char *) str:[ abCDefghCDij ] str:[ CD ] str_op:[ 2 ] >>>char *mystrcat(char *, const char *) str1:[ hello ] str2:[ world ] str_op:[ hello world ] >>>char *mystrncat(char *, const int, const char *) str1:[ hello ] str2:[ world ] num:[ 3 ] str_op:[ hello wor ] >>>int mylastindexof(const char *, const char *) str:[ aBcaBc ] chr:[ B ] str_op:[ 4 ] >>>char *mystrrev(char *) str:[ hello ] str_op:[ olleh ] >>>int mylastindexofstr(const char *, const char *) str1:[ abCDefghCDij ] str2:[ CD ] str_op:[ 2 ] >>>char *mysubstring(char *, const int, const char *) str1:[ ] str2:[ hello ] index:[ 3 ] str_op:[ lo ] >>>char *myftsubstring(char *, const int, const int, const char *) str1:[ ] str2:[ hello world ] from:[ 3 ] to:[ 8 ] str_op:[ lo wo ] >>>char *mytrimstr(char *) str:[ hello ] str_op:[ hello ] >>>int mystrcmp(const char *, const char *) str1:[ abcd ] str2:[ aBdc ] str_op:[ 1 ] >>>char *mytolowerstr(char *) str:[ abCDe ] str_op:[ abcde ] >>>char *mytoupperstr(char *) str:[ abCDe ] str_op:[ ABCDE ] >>>char mygetchrfromstr(const int, const char *) str:[ hello ] index:[ 3 ] str_op:[ l ] >>>int mystrsplit(char **, char *, const char) str:[ ab cd ef GH ] str_op:[ ab ] str_op:[ cd ] str_op:[ ef ] str_op:[ GH ] >>>char *mystrset(char *, const char) str:[ hello ] chr:[ A ] str_op:[ AAAAA ] >>>char *mystrnset(char *, const int, const char) str:[ hello ] chr:[ A ] num:[ 3 ] str_op:[ AAAlo ] >>>int mychricmp(const char, const char) chr1:[ a ] chr2:[ A ] str_op:[ 0 ] >>>int mystrncmpi(const char *, const int, const char *) str1:[ AAAbc ] str2:[ aaaBC ] num:[ 3 ] str_op:[ 0 ] >>>char *mystrmodchr(char *, const char, const char) str:[ abDcDE ] oldchr:[ D ] newchr:[ W ] str_op:[ abWcWE ] >>>char *mystrmodstr(char *, const char *, const char *) str:[ abCDEefCDErgfCDE ] oldstr:[ CDE ] newstr:[ HHH ] str_op:[ abHHHefHHHrgfHHH ] >>>char *mystrdup(const char *) str1:[ (null) ] str2:[ hello ] str_op:[ hello ] >>>char *mystrinsertchr(char *, const int, const char) str:[ hello ] index:[ 2 ] str_op:[ heWllo ] >>>char *mystrinsertstr(char *, const int, const char *) str:[ hello ] index:[ 2 ] str_op:[ heQQQllo ] >>>int mystrtoint(const char *) str:[ +1034 ] num:[ 0 ] str_op:[ 1034 ] >>>double mystrtodbl(const char *) str:[ +1034.66 ] num:[ 0.000000 ] str_op:[ 1034.660000 ] --------------------------------------------------------
原文:https://www.cnblogs.com/xinglichao/p/9158967.html