c语言字符串函数的实现
strlen函数
方法一:
#include
#include
using namespace std;
int my_strlen(const char *value)
{
assert(!value!=NULL);
int count = 0;
while (*value++ != NULL)
{
count++;
}
return count;
}
方法二:
int my_strlen(const char *value)
{
assert(!value != NULL);
char *pvalue = value;
while (*pvalue != NULL)
{
pvalue++;
}
return pvalue - value;
}
方法三:
int my_strlen(const char *value)
{
assert(value != NULL);
if (*value == '\0')
{
return 0;
}
else
{
return 1 + my_strlen(value + 1) ;
}
}
int main()
{
char *value = "xyz";
int i = my_strlen(value);
cout << i;
system("pause");
return 0;
}
my_strcpy函数的实现
#include
#include
using namespace std;
方法一:
char* my_strcmp(char *des,const char *src)//将src的值拷贝到des里面
{
assert(*src != NULL);
char *pdes = des;//内存保护,因为src为const所以不需要保护
while (*src != NULL)
{
*pdes++ = *src++;
}
*pdes = '\0';
return des;
}
方法二:
char* my_strcmp(char *des, const char *src)//将src的值拷贝到des里面
{
assert(*src != NULL);
char *pdes = des;//内存保护,因为src为const所以不需要保护
while ((*des++ = *src++));
return pdes;
}
int main()
{
char des[20] = { 0 };//定义一个较大的数组,防止出现内存不足
char *src = "abc";
char * res = my_strcmp(des, src);
cout << res;
system("pause");
return 0;
}
my_strcat函数的实现
#include
#include
using namespace std;
char* my_strcat(char *dest, const char *src)
{
assert((dest != NULL) && (src != NULL));
char *pdest = dest;
while (*pdest != '\0')
{
pdest++;
}
while (*src != NULL)
{
*pdest++ = *src++;
}
*pdest = '\0';
return dest;
}
int main()
{
char dest[20] = { 0 };
char *src = "abcde";
char * rest = my_strcat(dest, src);
cout << rest;
system("pause");
return 0;
}
my_strcmp函数的是实现
#include
#include
using namespace std;
方法一:
int my_strcmp(char *string1, char *string2)
{
assert(string1 != NULL&&string2 != NULL);
while (*string1++ != '\0'&&*string2++ != '\0')
{
if (*string1 > *string2)
{
return 1;
}
else if (*string1 < *string2)
{
return -1;
}
}
if (*string1 != '\0')
{
return 1;
}
else if (*string2 != '\0')
{
return -1;
}
return 0;
}
方法二:
int my_strcmp(char *string1, char *string2)
{
assert(string1 != NULL&&string2 != NULL);
int flag;
while (*string1 != '\0'||*string2 != '\0')
{
flag = *string1 - *string2;
if (flag != 0)
{
break;
}
string1++;
string2++;
}
if (flag > 0)
{
return 1;
}
else if (flag < 0)
{
return -1;
}
return 0;
}
int main()
{
char *a = "hello";
char *b = "hello";
int res = my_strcmp(a, b);
cout << res;
system("pause");
return 0;
}
my_strncpy函数的实现
char *strncpy(char *dest, const char *src, int n),把src所指向的字符串中以src地址开始的前n个字节复制到dest所指的数组中,并返回被复制后的dest。
#include
#include
using namespace std;
char* my_strncpy(char *dest,const char *src, int n)
{
assert(dest != NULL&&src != NULL&&n != 0);
char *pdest = dest;
while (n > 0)
{
*pdest++ = *src++;
n--;
}
return dest;
}
int main()
{
char a[20];
char *b="abcd";
int num;
cout << "请输入你想拷贝的字符数量:";
cin >> num;
char *res = my_strncpy(a, b, num);
cout << res;
system("pause");
return 0;
}
my_strncat函数的实现
strncat(str1,str2,n)主要功能是在字符串str1的结尾追加str2的前n个字符
#include
#include
using namespace std;
char* my_strncat(char *dest, const char *src, int n)
{
assert(dest != NULL&&src != NULL&&n != 0);
char *pdest = dest;
while (*pdest != '\0')
{
pdest++;
}
while (n > 0)
{
*pdest++ = *src++;
n--;
}
*pdest = '\0';
return dest;
}
int main()
{
char a[20] = "abc";
char *b = "def";
int n;
cout << "请输入你想连接的字符的大小:";
cin >> n;
char *res = my_strncat(a,b,n);
cout << res;
system("pasue");
return 0;
}
my_strncmp函数的实现
my_strncmp(str1,str2)
str1, str2 为需要比较的两个字符串,n为要比较的字符的数目。
#include
#include
using namespace std;
int my_strncmp(char *str1, char *str2, int n)
{
int i = 0;
int flag = 0;
while (*(str1 + i) != '\0' && *(str2 + i) != '\0')
{
if (i >= n)
{
break;
}
if (*(str1 + i) > *(str2 + i))
{
flag = 1;
break;
}
else if (*(str1 + i) < *(str2 + i))
{
flag = -1;
break;
}
else
{
flag = 0;
}
i++;
}
return flag;
}
int main()
{
char *a = "hello";
char *b = "hwllo";
int n;
cin >> n;
int i = my_strncmp(a, b, n);
cout << i;
system("pause");
return 0;
}