实现C语言字符串操作的库函数 包括基本的字符串复制 字符串长度 字符串比较等多种函数(C代码)

头文件

"mystring.h"

#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; }

复制前n个

//返回值:成功正  失败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 = ;
while ((i++ < num) && (*pd++ = *ps++))
;
if (--i == num){
return destin;
}
else{
for(++i; i > -; *pd-- = '\0', --i)
;
return NULL;
}
}

求字符串串长度

int mystrlen(const char *str){
if (!str){
return -;
}
const char *pstr = str;
while(*pstr++)
;
return (--pstr - str);
}

字符在字符串中第一次出现的index

int myindexof(const char *str, const char *chr){
if (!str || !chr ){
return -;
}
const char *pstr = str;
const char *pchr = chr;
char tmpc = '\0';
while((tmpc = *pstr++) != *pchr && tmpc)
;
if (!tmpc){
return -;
}
else{
return (--pstr - str);
}
}

字符串在字符串中第一次出现的index

int myindexofstr(const char *str, const char *substr){
if (!str || !substr){
return -;
}
const char *pstr = str;
const char *psubstr = substr;
int index = ;
while (*pstr){
if (*psubstr == *pstr){
++pstr;
if (*(++psubstr) == '\0'){
return index;
}
}
else{
pstr = pstr - (psubstr - substr) + ;
index = (pstr - str);
psubstr = substr;
}
}
return -;
}

拼接两个字符串

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;
}

将后字符串的前n个拼接到前字符串末尾

char *mystrncat(char *destin, const int n, const char *source){
if(!destin || !source || n > mystrlen(source) || n < ){
return NULL;
}
char *pd = destin;
const char *ps = source;
pd += mystrlen(destin);
for(int i = ; i < n; ++i){
*pd++ = *ps++;
}
*pd = '\0';
return destin;
}

字符在字符串中最后一次出现的index

int mylastindexof(const char *str, const char *chr){
if(!str || !chr){
return -;
}
const char *pstr = str;
const char *pchr = chr;
pstr += mystrlen(str);
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 - );
for(int i = ; i < (length / ); ++i){
static char tmp;
tmp = *pstr;
*pstr++ = *pend;
*pend-- = tmp;
}
return str;
}

字符串在字符串中最后一次出现的index

int mylastindexofstr(const char *str, const char *substr){
if(!str || !substr){
return -;
}
const char *pstr = str;
const char *psubstr = substr;
int strlength = mystrlen(str);
int sublength = mystrlen(substr);
pstr += (strlength - );
psubstr += (sublength - );
int j_sub = ;
int endindex = strlength - ;
for(int i = ; i < strlength; ++i){
if(*pstr == *psubstr){
--pstr;
--psubstr;
if(++j_sub == sublength){
return (endindex - sublength + );
}
}else{
pstr += (j_sub - );
psubstr = substr + sublength- ;
endindex = (pstr - str);
}
}
return -;
}

获得字符串从index开始到末尾的子串

char *mysubstring(char *tosubstr, const int begin_index, const char *str){
if(!tosubstr || !str || begin_index > \
mystrlen(str) || begin_index < ){
return NULL;
}
char *ptosub = tosubstr;
const char *pstr = str;
pstr += begin_index;
while((*ptosub++ = *pstr++))
;
return tosubstr;
}

获得字符串从f_index开始到t_index的子串

char *myftsubstring(char *tosubstr, const int begin_index,    //左闭右开
const int end_index, const char *str){
if(!tosubstr || !str || begin_index >= end_index \
|| begin_index < || 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) - );
while(*p1++ == ' ')
;
while(*p2-- == ' ')
;
for((--p1, ++p2);p1 <= p2; (*pstr++ = *p1++))
;
*pstr = '\0';
return str;
}

字符串比较(对应的字符ASCII值的比较)

int mystrcmp(const char *str1, const char *str2){
if(!str1 || !str2){
return -;//-2表示没法比较
}
const char *pstr1 = str1;
const char *pstr2 = str2;
int flag = ;
while((*pstr1) && (*pstr2)){
if(*pstr1 < *pstr2){
flag = -;
break;
}else if(*pstr1 > *pstr2){
flag = ;
break;
}
++pstr1;
++pstr2;
}
if(!(*pstr1) && !(*pstr2)){
flag = ;
}else if(!(*pstr1)){
flag = -;
}else if(!(*pstr2)){
flag = ;
}
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;
}

从字符串中获得指定index的字符

char mygetchrfromstr(const int index, const char *str){
if(!str || index < || 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 -;
}
char **pd = destin;
char *ps = source;
int flag = ;
int sub_num = ;
while(*ps){
if(*ps != lmt_chr){
if(!flag){
*pd++ = ps;
++sub_num;
}
flag = ;
}else{
*ps = '\0';
flag = ;
}
++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;
}

将字符串中前n个字符设置为指定字符

char *mystrnset(char *str, const int num, const char set_chr){
if(!str || !set_chr || num < || num > mystrlen(str)){
return NULL;
}
char *pstr = str;
for(int i = ; i < num; (*pstr++ = set_chr), ++i)
;
return str;
}

忽略大小写进行字符串比较

int mychricmp(const char chr1, const char chr2){
if(!chr1 || !chr2){
return -;
}
int diff = chr1 - chr2;
if(diff == || (diff == ('a' - 'A')) || (diff == ('A' - 'a'))){
return ;
}else if(diff < ){
return -;
}else{
return ;
}
}

忽略大小写进行字符串前n个字符的比较

int mystrncmpi(const char *str1, const int num, const char *str2){
if(!str1 || !str2 || num <= ||\
num > mystrlen(str1) || num > mystrlen(str2)){
return -;
}
const char *pstr1 = str1;
const char *pstr2 = str2;
for(int i = ; i < num; ++i){
int flag = mychricmp(*pstr1++, *pstr2++);
if(flag == -){
return -;
}else if(flag == ){
return ;
}else if(flag == -){
return -; //失败
}
}
return ; }

修改字符串中全部指定的字符为新的字符

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 = ;
while((index = myindexofstr(pstr, old_str)) != -){
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 + ), sizeof(char)))){
return NULL;
}
if(!(mystrcpy(destin, source))){
return NULL;
}
return destin;
}

在字符串的指定index处插入字符

char *mystrinsertchr(char *str, const int index, const char chr){
int str_length = mystrlen(str);
if(!str || index < || index > str_length){ //支持插入'\0' 允许插在串尾
return NULL;
}
char *pstr = str, *lastp;
pstr += str_length;
lastp = pstr + ;
for(int i = ; i <= (str_length - index); (*lastp-- = *pstr--), ++i)
;
*(++pstr) = chr;
return str;
}

在字符串的指定index处插入字符串

char *mystrinsertstr(char *str, const int index, const char *insert_str){
int str_length = mystrlen(str);
if(!str || !insert_str || index < || 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 = ; i <= (str_length - index); (*lastp-- = *pstr--), ++i)
;
for(int i = ; i < insert_str_length; (*(++pstr) = *pinsert_str++), ++i)
;
return str;
}

数字字符串转int类型整数

int mystrtoint(const char *int_str){
if(!int_str){
fprintf(stderr, "error: input str pointer is null\n");
return ;
}
const char *pint_str = int_str;
for(; *pint_str == ' ' || *pint_str == '\t' ||\
*pint_str == '\r' || *pint_str == '\n'; ++pint_str) //跳过前面的空格、制表符、换行符
;
int sign = ;
if(*pint_str == '-' || *pint_str == '+'){
*pint_str == '-' ? (sign = -) : (sign = );
++pint_str;
}
int the_intnum = ;
//没做英文字符的处理 那显然不是纯数字的字符串
for(; *pint_str; (the_intnum = (*pint_str - '') + * the_intnum), ++pint_str)
;
return (sign * the_intnum);
}

数字字符串转double类型浮点数

double mystrtodbl(const char *dbl_str){
if(!dbl_str){
fprintf(stderr, "error: input str pointer is null\n");
return ;
}
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 - '') + 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 - '') * 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[] = {};
printf("str:[ %s ]\n", str);
printf("soc:[ %s ]\n", "hello world");
mystrcpy(str, "hello world");
printf("str_op:[ %s ]\n", str);
}

复制前n个

void test_mystrncpy(){
printf("\n>>>char *mystrncpy(char *, const int, const char *)\n");
char str[] = {};
printf("str:[ %s ]\n", str);
printf("soc:[ %s ]\n", "hello world");
printf("num:[ %d ]\n", );
mystrncpy(str,, "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));
}

字符在字符串中第一次出现的index

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));
}

字符串在字符串中第一次出现的index

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[] = {'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);
}

将后字符串的前n个拼接到前字符串末尾

void test_mystrncat(){
printf("\n>>>char *mystrncat(char *, const int, const char *)\n");
char str[] = {'h', 'e', 'l', 'l', 'o', ' '};
char *p = "world";
printf("str1:[ %s ]\n", str);
printf("str2:[ %s ]\n", p);
printf("num:[ %d ]\n", );
mystrncat(str, , p);
printf("str_op:[ %s ]\n", str);
}

字符在字符串中最后一次出现的index

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[] = {'h', 'e', 'l', 'l', 'o', ' '};
printf("str:[ %s ]\n", str);
mystrrev(str);
printf("str_op:[ %s ]\n", str);
}

字符串在字符串中最后一次出现的index

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", mylastindexofstr(p1, p2));
}

获得字符串从index开始到末尾的子串

void test_mysubstring(){
printf("\n>>>char *mysubstring(char *, const int, const char *)\n");
char str[] = {};
char *p = "hello";
printf("str1:[ %s ]\n", str);
printf("str2:[ %s ]\n", p);
printf("index:[ %d ]\n", );
mysubstring(str, , p);
printf("str_op:[ %s ]\n", str);
}

获得字符串从f_index开始到t_index的子串

void test_myftsubstring(){
printf("\n>>>char *myftsubstring(char *, const int, const int, const char *)\n");
char str[] = {};
char *p = "hello world";
printf("str1:[ %s ]\n", str);
printf("str2:[ %s ]\n", p);
printf("from:[ %d ]\n", );
printf("to:[ %d ]\n", );
myftsubstring(str, , , 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);
}

字符串比较(对应的字符ASCII值的比较)

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[] = {'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[] = {'a', 'b', 'C', 'D', 'e'};
printf("str:[ %s ]\n", str);
mytoupperstr(str);
printf("str_op:[ %s ]\n", str);
}

从字符串中获得指定index的字符

void test_mygetchrfromstr(){
printf("\n>>>char mygetchrfromstr(const int, const char *)\n");
char *p = "hello";
printf("str:[ %s ]\n", p);
printf("index:[ %d ]\n", );
printf("str_op:[ %c ]\n", mygetchrfromstr(, p));
}

以指定字符切割字符串

void test_mystrsplit(){
printf("\n>>>int mystrsplit(char **, char *, const char)\n");
char *p[] = {};
char **p1 = p;
char str[] = {};
mystrcpy(str, " ab cd ef GH ");
printf("str:[ %s ]\n", str);
int num = mystrsplit(p, str, ' '); //ÒÔ¿Õ¸ñÇиî
for(int i = ; i < num; ++i){
printf("str_op:[ %s ]\n", *p1++);
}
}

将字符串中全部字符设置为指定字符

void test_mystrset(){
printf("\n>>>char *mystrset(char *, const char)\n");
char str[] = {'h', 'e', 'l', 'l', 'o'};
printf("str:[ %s ]\n", str);
printf("chr:[ %c ]\n", 'A');
mystrset(str, 'A');
printf("str_op:[ %s ]\n", str);
}

将字符串中前n个字符设置为指定字符

void test_mystrnset(){
printf("\n>>>char *mystrnset(char *, const int, const char)\n");
char str[] = {'h', 'e', 'l', 'l', 'o'};
printf("str:[ %s ]\n", str);
printf("chr:[ %c ]\n", 'A');
printf("num:[ %d ]\n", );
mystrnset(str, , '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));
}

忽略大小写进行字符串前n个字符的比较

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", );
printf("str_op:[ %d ]\n", mystrncmpi(p1, , p2));
}

修改字符串中全部指定的字符为新的字符

void test_mystrmodchr(){
printf("\n>>>char *mystrmodchr(char *, const char, const char)\n");
char str[] = {'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[] = {};
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;
}

在字符串的指定index处插入字符

void test_mystrinsertchr(){
printf("\n>>>char *mystrinsertchr(char *, const int, const char)\n");
char str[] = {'h', 'e', 'l', 'l', 'o'};
printf("str:[ %s ]\n", str);
printf("index:[ %d ]\n", );
mystrinsertchr(str, , 'W');
printf("str_op:[ %s ]\n", str);
}

在字符串的指定index处插入字符串

void test_mystrinsertstr(){
printf("\n>>>char *mystrinsertstr(char *, const int, const char *)\n");
char str[] = {'h', 'e', 'l', 'l', 'o'};
printf("str:[ %s ]\n", str);
printf("index:[ %d ]\n", );
char *p = "QQQ";
mystrinsertstr(str, , p);
printf("str_op:[ %s ]\n", str);
}

数字字符串转int类型整数

void test_mystrtoint(){
printf("\n>>>int mystrtoint(const char *)\n");
char *p = " +1034";
int num = ;
printf("str:[ %s ]\n", p);
printf("num:[ %d ]\n", num);
num = mystrtoint(p);
printf("str_op:[ %d ]\n", num);
}

数字字符串转double类型浮点数

void test_mystrtodbl(){
printf("\n>>>double mystrtodbl(const char *)\n");
char *p = " +1034.66";
double num = ;
printf("str:[ %s ]\n", p);
printf("num:[ %lf ]\n", num);
num = mystrtodbl(p);
printf("str_op:[ %lf ]\n", num);
}

整体测试

main.c

#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 ;
}

结果

__________________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:[ 8 ] >>>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 ] -------------------------------------------------------
上一篇:input输入框 只能输入数字 oninput = "value=value.replace(/[^\d]/g,'')" input输入框输入大于0的数字 oninput="value=value.replace(/\D|^0/g,'')"


下一篇:MpVue开发之swiper的使用