#include"Head.h"
#include<assert.h>
#include<string>
char *_strcpy(char *dst, const char *src)
{
assert(dst != NULL&&src != NULL);
char *ret = dst;
while ((*dst++ = *src++) != '\0');
return ret;
}
int _mystrcmp(char *dst, const char *src)
{
assert(dst != NULL&&src != NULL);
while (*dst&&*src&&*dst==*src)
{
++dst;
++src;
}
if (*dst > *src)
return 1;
if (*dst < *src)
return -1;
else
return 0;
}
char *_strcat(char *source, int s1,const char *dest)
{
assert(source != NULL&&dest != NULL);
int i, j;
i = s1;
for (j = 0; dest[j] != 0; j++, i++)
{
source[i] = dest[j];
}
return source;
}
int _strlen(const char *_Str)
{
assert(_Str != NULL);
int _Size = 0;
while (*_Str++ !='\0')
{
++_Size;
}
return _Size+1;
}
char *_strstr(char *str, char *substr)
{
assert( str!= NULL&& substr!= NULL);
char *pTempSrc = str;
char *pTempsub = substr;
do
{
if (*pTempSrc != *pTempsub++)
{
pTempsub = substr;
}
if (!*pTempsub)
{
return pTempSrc - (pTempsub - substr) + 1;
}
} while (*pTempSrc++);
return "a";
}
char *_strtok(char *str, const char *delim)
{
static char *src = NULL;
const char *indelim = delim;
int flag = 1, index = 0;
char *temp = NULL;
if (str == NULL)
{
str = src;
}
for (; *str; str++)
{
delim = indelim;
for (; *delim; delim++)
{
if (*str == *delim)
{
*str = NULL;
index = 1;
break;
}
}
if (*str != NULL&&flag == 1)
{
temp = str;
flag = 0;
}
if (*str != NULL&&flag == 0 && index == 1)
{
src = str;
return temp;
}
}
src = str;
return temp;
}
char *_strchr(char *str, char character)
{
if (str == NULL)
return NULL;
char *p = NULL;
while (*str!='\0')
{
if (*str == character)
p = str;
str++;
}
return p;
}
void main()
{
cout << "实现了strcpy: " << endl;
char s[10];
cout <<"拷贝 "<< _strcpy(s,"1234665") << endl;
cout << "实现了mystrcmp: " << endl;
char l[10] = "sdfa55";
char f[10] = "22sdfa";
cout <<"比较l和f "<<"结果为: "<< _mystrcmp(l,f) << endl;
cout << "实现了strcat:" << endl;
char l1[13] = "sdfa55";
char f1[10] = "22sdfa";
cout <<"拼接 "<<_strcat(l1,6,f1) << endl;
cout << "实现了_strlen:" << endl;
char l2[10] = "fasdfasdd";
cout <<"长度 " <<_strlen(l2) << endl;
cout << "实现了strstr: " << endl;
char *p = _strstr("114145", "14");
cout <<"搜寻子char:::" <<p << endl;
cout << "实现了strtok: " << endl;
char s1[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";
char *delim = "-";
char *p1;
cout << _strtok(s1, delim) << endl;
while ((p1=_strtok(NULL,delim)))
cout << p1 << endl;
cout << "实现了strchr: " << endl;
char u[] = "fiaslkal55";
cout << _strchr(u, 'i') << endl;;
system("pause");
}