练习题目
封装函数MyStrlen实现获得字符串长度的功能
封装函数MyStrcpy实现字符串拷贝的功能
封装函数MyStrcat实现字符串拼接的功能
封装函数MyStrcmp实现字符串比较的功能
#include<stdio.h>
int MyStrlen(char *pstr)
{
int n = 0;
while(*pstr != '\0')
{
pstr++;
n++;
}
return n;
}
int MyStrcpy(char *pstr, char *pdst)
{
while(*pdst != '\0')
{
*pstr = *pdst;
pstr++;
pdst++;
}
return 0;
}
int MyStrcat(char *pstr, char *pdst)
{
while(*pstr != '\0')
{
pstr++;
}
while(*pdst != '\0')
{
*pstr = *pdst;
pdst++;
pstr++;
}
*pstr = '\0';
return 0;
}
int MyStrcmp(char *pstr, char *pdst)
{
while(*pstr != '\0' && *pdst != '\0')
{
if(*pstr == *pdst)
{
pstr++;
pdst++;
}
else
{
break;
}
}
return *pstr - *pdst;
}
int main(void)
{
char str[32] = {0};
char dst[32] = {0};
char a[32] = {0};
int b = 0;
int len = 0;
gets(str);
gets(dst);
len = MyStrlen(str);
MyStrcpy(a, dst);
MyStrcat(str, dst);
b = MyStrcmp(str, dst);
printf("len = %d\n", len);
printf("dst拷贝到a = %s\n", a);
printf("dst拼接str后 = %s\n", str);
printf("此时str和dst分别是:%s ------%s\n", str, dst);
printf("str和dst比较后 = %d\n", b);
return 0;
}
题目二
封装函数实现传入一个字符串,实现字符串的倒置
#include<stdio.h>
//获取字符串长度
int MyStrlen(char *pstr)
{
int n = 0;
while(*pstr != '\0')
{
pstr++;
n++;
}
return n;
}
//对字符串进行倒置
int Strlnvert(char *pstr, char *pdst, int len)
{
while(*pstr != '\0')
{
*(pdst+len-1) = *pstr;
pstr++;
pdst--;
}
return 0;
}
//主函数
int main(void)
{
char str[32] = {0};
char dst[32] = {0};
gets(str);
Strlnvert(str, dst, MyStrlen(str));
printf("倒置后= %s\n", dst);
return 0;
}