实现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 = 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);
}
字符在字符串中第⼀次出现的index
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);
}
}
字符串在字符串中第⼀次出现的index
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{
pstr = pstr - (psubstr - substr) + 1;
index = (pstr - str);
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;
}
将后字符串的前n个拼接到前字符串末尾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;
}
字符在字符串中最后⼀次出现的index
int mylastindexof(const char *str, const char *chr){
if(!str || !chr){
return -1;
}
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 - 1);
for(int i = 0; i < (length / 2); ++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 -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{
pstr += (j_sub - 1);
psubstr = substr + sublength- 1;
endindex = (pstr - str);
}
}
return -1;
}
获得字符串从index开始到末尾的⼦串
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;
}
获得字符串从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 < 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;
}
字符串⽐较(对应的字符ASCII值的⽐较)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;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。