c语言,不调用string库函数,怎样将两个字符串连接起来?

2024-11-25 08:13:57
推荐回答(3个)
回答1:

void _strcat(char *dest,char *sorc)
{
while(*dest) dest++; //首先目标字符串指针指到末尾
while(*dest++=*sorc ++); //赋值...直到sorc为空
}

回答2:

#include <stdio.h>
#include <stdlib.h>
char* mystrcat(char* s1, char* s2)
{
    char* tmp1 = s1;
    char* tmp2 = s2;
    for (; *tmp1; ++tmp1){}
    while ( (*tmp1++ = *tmp2++) ){}
    return s1;
}
int main(void)
{
    char sz1[100] = "abc123";
    char sz2[]    = "okok456";
    mystrcat(sz1, sz2);
    printf("sz1=%s\n", sz1);
    return 0;
}

#include

#include


char* mystrcat(char* s1, char* s2)

{

char* tmp1 = s1;

char* tmp2 = s2;


for (; *tmp1; ++tmp1){}


while ( (*tmp1++ = *tmp2++) ){}


return s1;

}


int main(void)

{

char sz1[100] = "abc123";

char sz2[]    = "okok456";


mystrcat(sz1, sz2);


printf("sz1=%s\n", sz1);


return 0;

}

回答3:

strcat