# strlen.strcpy.strcmp.stratoi.stritoa的自己实现

#include <stdio.h>
#include <string.h>

//1.字符串连接
char * Mystrcpy(char *des,const char *src,char *p)
{
while(*p++ = *src++);      //指针p指向des的'\0'位置，从此处开始复制，即实现了字符串连接
return des;
}

//2.字符串比较
int Mystrcmp(const char *arr,const char *brr)
{
int count = 0;
int tmp = 0;
int i = 0;
do
{
if(arr[i] > brr[i])
{
tmp = 1 ;
break;
}
else if(arr[i] < brr[i])
{
tmp = -1;
break;
}
i++;
}while(arr[i]!='\0');
return tmp;
}

int main()
{
char arr[] = "And";
char brr[] = "Aid";
printf("%d\n",Mystrcmp(arr,brr));

return 0;
}

//3.将字符串转换成数字
int Obtainnum(const char *arr)      //获得个数
{
int count = 0;
while(*arr != '\0')
{
count++;
arr++;
}
return count;
}

int Mystratoi(const char *arr)
{
int count = Obtainnum(arr);
int sum = 0;
int t = 1;
int d;
for(int i = 1;i<count;i++)
{
t *= 10;
}
do
{
d = *arr-'0';
sum += d*t;
t /= 10;
arr++;
count--;
}while(count != 0);

return sum;
}

//4.将数字转换成字符串
void Myitoa(char *str,int n)
{
int count = 0;
int t = n;
do
{
n /= 10;
count++;
}while(n != 0);
str[count] = '\0';

for(;count-1 >= 0;count--)
{
n = t%10;
str[count-1] = '0'+n;
t /= 10;
}
}

//5.计算字符串的有效长度，不包含'\0'
int Mystrlen(const char *str)
{
assert(str != NULL);
if(str == NULL)
{
return 0;
}
int count = 0;
while(*str != '\0')
{
count++;
str++;
}

return count;
}

int main()
{
char des[20] = "abcd";
char src[] = "efghasdsa";
int len_1 = strlen(des);
int len_2 = strlen(src);
//printf("%s\n",Mystrcpy(des,src,&des[len_1]));   //字符串连接测试
//printf("%d\n",Mystrcmp(des,src));   //字符串比较测试
char arr[] = "1234";               //字符串转换为数字
//printf("%d\n",Mystratoi(arr));
char str[10];                                     //数字转换为字符串
Myitoa(str,12345);
printf("%s\n",str);
printf("%d\n",Mystrlen(des));                     //计算字符串有效长度

return 0;
}