#include<stdio.h>
#include<string.h>
#include<assert.h>
void Mystrncpy(char *des,char *src,int count)
{
int i;
for(i=0;i<count&&src[i]!='\0';i++)
{
des[i] = src[i];
}
while(i < count)
{
des[i++] = '\0';
}
printf("%s\n",des);
}
void Mystrncat(char *des,char *src,int count)
{
char *cp = des;
assert(src && des);
while(*des != '\0')
{
*des++;
}
while(count && *src)
{
*des++ = *src++;
count--;
}
*des = '\0';
printf("%s\n",cp);
}
void Mystrncmp(const char *str1,const char *str2,int count)
{
assert(str1 && str2);
while(count && (*str1 == *str2))
{
if(str1 == '\0')
{
break;
}
str1++;
str2++;
count--;
}
printf("%d\n",*str1-*str2);
}
int main()
{
//字符串拷贝测试用例
char str1[10];
char *str2 = "abcde";
Mystrncpy(str1,str2,8);
//字符串连接测试用例
char str3[20] = {"abc"};
char *str4 = "xyz";
Mystrncat(str3,str4,3);
//字符串比较测试用例
char *arr1 = "abcde";
char *arr2 = "abcdef";
Mystrncmp(arr1,arr2,strlen(arr1));
//每个测试我都测试了很多,鉴于美观,这里只显示一个测试用例
return 0;
}
限制长度的字符串函数
最新推荐文章于 2023-10-01 21:35:52 发布