我自己的类似strcat()函数只打印一个字符数组
我一直在练习编程,所以我决定自己尝试键入strcat()函数,或者类似的函数。我输入这段代码是为了继续,但我不知道问题出在哪里我自己的类似strcat()函数只打印一个字符数组,c,arrays,string.h,C,Arrays,String.h,我一直在练习编程,所以我决定自己尝试键入strcat()函数,或者类似的函数。我输入这段代码是为了继续,但我不知道问题出在哪里 #include <stdio.h> #include <stdlib.h> void main(){ int i, j; char a[100]; char b[100]; printf("enter the first string\n"); scanf("%s", &a); printf("en
#include <stdio.h>
#include <stdlib.h>
void main(){
int i, j;
char a[100]; char b[100];
printf("enter the first string\n");
scanf("%s", &a);
printf("enter the second string\n");
scanf("%s", &b);
for(i =0; i<100; i++){
if(a[i] == '\0')
break;
}
// printf("%d", i);
for(j = i; j<100; j++){
a[j+1] = b[j-i];
if(b[j-i] == '\0')
break;
}
printf("%s", a);
}
#包括
#包括
void main(){
int i,j;
字符a[100];字符b[100];
printf(“输入第一个字符串\n”);
scanf(“%s”、&a);
printf(“输入第二个字符串\n”);
scanf(“%s”、&b);
对于(i=0;i实现strcat
作为“原始字节复制循环”并不难,只需执行以下操作:
#include <stdio.h>
#include <string.h>
char* strdog (char* restrict s1, const char* restrict s2)
{
s1 += strlen(s1); // point at s1 null terminator and write from there
do
{
*s1 = *s2; // copy characters including s2's null terminator
s1++;
} while(*s2++ != '\0');
return s1;
}
int main(void)
{
char dst[100] = "hello ";
char src[] = "world";
strdog(dst, src);
puts(dst);
}
#包括
#包括
char*strdog(char*restrict s1,const char*restrict s2)
{
s1+=strlen(s1);//指向s1空终止符并从此处写入
做
{
*s1=*s2;//复制包含s2的空终止符的字符
s1++;
}而(*s2++!='\0');
返回s1;
}
内部主(空)
{
char dst[100]=“你好”;
char src[]=“世界”;
strdog(dst、src);
看跌期权(dst);
}
专业图书馆将在“对齐的数据块”上进行复制请在此处添加您的代码。请在此处发布您的问题,而不是代码图片。请在此处以文本形式发布您的代码,包括所有错误和输出。这样可以使此处的人员更容易调试代码,并保留一份记录,以防在fu中出现类似问题turescanf(“%s”,a)第二个循环以j=i
NUL在a
中的位置开始。但是您将b
的值分配给a[j+1]
,因此更改的第一个字符是a[i+1]
。因此NUL保留在原来的位置,您在打印a
时看不到效果