ex5_4.cpp
来自「谭浩强 c程序学习时的程序 希望对初学者有借鉴意义」· C++ 代码 · 共 64 行
CPP
64 行
#include <stdio.h>
#include <string.h>
void strcat(char *s,char *t)
{
s=s+strlen(s);
while(*s++=*t++)
;
}
void strncpy(char *s, char *t,int n)
{
int i=1;
while (i<=n && (*s++ = *t++) != '\0')
i++;
if(*(s-1)!='\0')
*s='\0';
}
void strncat(char *s,char *t,int n)
{
int i=1;
s=s+strlen(s);
while(i<=n && (*s++=*t++)!='\0')
i++;
if(*(s-1)!='\0')
*s='\0';
}
int strncmp(char *s, char *t,int n)
{
int i;
for (i = 1; *s == *t && i<=n; s++,t++,i++)
if (*s == '\0' || i==n)
return 0;
return *s - *t;
}
void main()
{
char s[100],t[100],s_temp[100];
int res,n;
printf("please input string s:");
gets(s);
printf("please input string t:");
gets(t);
printf("please input n:");
scanf("%d",&n);
strcpy(s_temp,s);
strcat(s,t);
printf("strcat result is %s\n",s);
strcpy(s,s_temp);
strncpy(s,t,n);
printf("strncpy result is %s\n",s);
strcpy(s,s_temp);
strncat(s,t,n);
printf("strncat result is %s\n",s);
strcpy(s,s_temp);
res=strncmp(s,t,n);
printf("strncmp result is %d\n",res);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?