#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//字符串的长度
int mystrlen(const char *str)
{
int len = 0;
if (str == NULL)
{
return NULL;
}
while (*str++ != '\0')
{
len++;
}
return len;
}
//字符串拷贝函数
char *mystrcpy(char *dstr, char *str)
{
if (dstr == NULL || str == NULL)
return NULL;
char *address = dstr;
while (*dstr++ = *str++)
;
return address;
}
//字符串的长度受到限制的拷贝
char *mystrncpy(char *dstr, const char *str, int n)
{
if (dstr == NULL || str == NULL)
return NULL;
char *ncpy = dstr;
while (n--)
{
if (*str != '\0')
{
*dstr++ = *str++;
}
else
{
* dstr++ = '\0';
}
}
return ncpy;
}
//两个字符串的连接
char * mystrcat(char *dstr, char *str)
{
if (dstr == NULL || str == NULL)
return NULL;
char *address = dstr;
while (*dstr != '\0')
{
*dstr++;
}
while ((*dstr++ = *str++) != '\0')
;
return address;
}
char *mystrncat(char *dstr, char *str,int n)
{
if (dstr == NULL || str == NULL)
return NULL;
char * address = dstr;
while (*dstr != '\0')
{
*dstr++;
}
while (n-- && *str != '\0')
{
*dstr++ = *str++;
}
return address;
}
//查找字符串中首次出现字符C的位置,找到返回指针,找不到返回NULL
char * mystrchr(char *str, char c)
{
if (str == NULL)
return NULL;
for (; *str != c; str++)
{
if (*str == '\0')
return NULL;
}
}
//查找字符串中最后一次出现字符c的位置,找到返回指针,找不到返回NULL
char *mystrrchr(char *str, char c)
{
if (str == NULL)
return NULL;
char *p = NULL;
while (*str != '\0')
{
if (*str == (char)c)
{
p = (char*)str;
}
str++;
}
return p;
}
//字符串查找给定字符,第N次出现的位置
char * mystrnchr(char * dstr, char c, int n)
{
if (dstr == NULL)
return NULL;
int count = 0;
while (*dstr++ != '\0')
{
if (*dstr == c)
{
count += 1;
}
if (count == n)
{
return (char*)dstr;
}
}
return NULL;
}
//字符查找(这是一般的查找,后面再加KMP算法)
char *mystrstr(char *dstr, char *str)
{
if (dstr == NULL || str == NULL)
return NULL;
char *d = dstr;
char *s = str;
for (; *dstr != '\0'; ++dstr)
{
for (d = dstr, s = str; *s != '\0' && *d == *s; ++d, ++s)
{
;
}
if (*s == '\0')
{
return (char*)dstr;
}
}
return NULL;
}
//字符串比较
int mystrcmp(char *dstr, char *str)
{
if (dstr == NULL || str == NULL)
return NULL;
while (*dstr && *str && *dstr == *str)
{
++dstr;
++str;
}
return (*dstr - *str);
}
//加限制的字符串比较
int mystrncmp(char * dstr, char *str,int n)
{
if (dstr == NULL || str == NULL)
return NULL;
while (*dstr && *str && *dstr == *str && n--)
{
++dstr;
++str;
}
return (*dstr - *str);
}
int main()
{
/*
char dstr[100] = "";
char *str = "hello word";
int n = mystrlen(str);
char *p = mystrcpy(dstr,str);
cout << "字符串的长度:"<<n<< endl;
cout << "拷贝后的字符串:" << p<<endl;
*/
/*
char dstr[100] = "";
char cstr[100] = "ab";
char fstr[100] = "cc";
char *str = "hello word";
cout << "str受限拷贝:" << mystrncpy(dstr,str,5)<<endl;
cout << "str连接后:" << mystrcat(cstr,fstr)<<endl;
cout << "字符串受限后连接:" << mystrncat(fstr,str,3)<<endl;
/
char *str = "hello a worad";
char *ctr = "hello waaoard";
cout << "首次查找字符:" << mystrchr(str,'a')<< endl;
cout << "最后查找字符:" << mystrrchr(str,'a') << endl;
cout << "查找第n次字符:" << mystrnchr(ctr,'a',2) << endl;
/
char *dstr = "hello world";
char *str = "world";
cout << "查找的字符串:" << mystrstr(dstr, str) << endl;;
*/
//
char *dstr = "hellocworld";
char *str = "hellobworld";
cout << "比较字符串:" << mystrcmp(dstr, str)<< endl;
cout << "加限制后字符串比较:" << mystrncmp(dstr, str, 4) << endl;
return 0;
}
//
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
class Soulation
{
public:
void ReplaceSpace(char *str, int len)
{
if (str == NULL || len < 0)
return;
int i = 0;
int oldnumber = 0;
int replacenumber = 0;
while (str[i] != '\0')
{
oldnumber++;
if (str[i] == ' ')
{
replacenumber++;
}
i++;
}
int newlen = oldnumber + replacenumber * 2;
if (newlen > len)
{
return;
}
int poldlen = oldnumber;
int pnewlen = newlen;
while (pnewlen > poldlen && poldlen >= 0)
{
if (str[poldlen] = ' ')
{
str[pnewlen--] = '0';
str[pnewlen--] = '2';
str[pnewlen--] = '%';
}
else
{
str[newlen--] = str[poldlen];
}
}
--poldlen;
}
};
///