自实现strcat strcpy strcmp函数记录
strcat :将两个字符串连接
strcpy:复制字符串
strcmp:比较字符串
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char * m_strcat(char * dest, const char * source)
{
//使用指针记录开始位置
char * tmp = dest;
while (*tmp != '\0')
{
//将指针移到末尾
tmp++;
}
while (*source != '\0')
{
*tmp = *source;
tmp++;
source++;
}
return dest;
}
char * m_strcpy(char * dest, const char * source)
{
if (dest == source)
{
return dest;
}
char * tmp = dest;
while (*source != '\0')
{
*tmp = *source;
tmp++;
source++;
}
*tmp = '\0';
return dest;
}
//应分情况,返回-1 0 1
int m_strcmp(char * s1, char * s2)
{
if (s1 == NULL || s2 == NULL)
{
return 0;
}
if (strlen(s1) != strlen(s2))
{
return 0;
}
while (*s1 != '\0')
{
if (*s1 != *s2)
{
return 0;
}
s1++;
s2++;
}
return 1;
}
int main()
{
char s1[10] = { "acbs" };
char s2[20] = { "xxx" };
cout << m_strcat(s1, s2) << endl;
cout << s2 << endl;
m_strcpy(s1, s2);
cout << s1 << endl;
m_strcmp(s1, s2);
char s3[10] = { "aaaa" };
char s4[10] = { "aaa" };
cout << m_strcmp(s3, s4) << endl;
}