实现C和C++字符串函数 (1) c实现: strlen;strcat;strcpy;strcmp... (2) c++实现:封装上述函数
根据#include<stdio.h>#include<string.h>intmain(void){intn;chars1[]="c++";chars2[]="Iaml...
根据#include <stdio.h>
#include <string.h>
int main (void)
{
int n;
char s1[]="c++";
char s2[]="I am learning";
char s3[80];
strlen(s1);
printf("将s1字符的长度%d\n",strlen (s1));//得到字符的长度
strcat(s1,s2);
printf ("将s1与s2合并在一起%s\n",strcat(s1,s2));//将s1复制到s2后面,输出 I am learning c++
strcpy(s3,s1);
printf("将s1字符全部复制到s3中%s\n:",strcpy(s3,s1));//复制字符串
n=strcmp(s1,s2);//比较s1与s2字符串的大小
printf ("s1与s2的比较结果:\n");
if (n==0)
printf("s1==s2");
if (n>0)
printf("s1>s2");
if (n<0)
printf("s1<s2");return 0;
} 展开
#include <string.h>
int main (void)
{
int n;
char s1[]="c++";
char s2[]="I am learning";
char s3[80];
strlen(s1);
printf("将s1字符的长度%d\n",strlen (s1));//得到字符的长度
strcat(s1,s2);
printf ("将s1与s2合并在一起%s\n",strcat(s1,s2));//将s1复制到s2后面,输出 I am learning c++
strcpy(s3,s1);
printf("将s1字符全部复制到s3中%s\n:",strcpy(s3,s1));//复制字符串
n=strcmp(s1,s2);//比较s1与s2字符串的大小
printf ("s1与s2的比较结果:\n");
if (n==0)
printf("s1==s2");
if (n>0)
printf("s1>s2");
if (n<0)
printf("s1<s2");return 0;
} 展开
1个回答
2014-01-12
展开全部
size_t strlen(const char *s)
{
char *ts;
for(ts =s; *s; s++)
;
return s-ts;
}
char *strcpy(char *dest, const char *src)
{
char *ts =dest;
for(; *dest++ = *src++; )
;
return ts;
}
{
char *ts;
for(ts =s; *s; s++)
;
return s-ts;
}
char *strcpy(char *dest, const char *src)
{
char *ts =dest;
for(; *dest++ = *src++; )
;
return ts;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询