C language simulation to realize Strcat function

analyze

Strcat String concatenation function
Implement char * my_strcat(char * str1, const char * str2) function
to return the address of str1 string.
Append the string pointed to by str2 to the string pointed to by str1. The
write picture description here
code is as follows

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char * my_strcat( char* str1,const char* str2)
{
    char *ret = str1;
    assert(str1!=NULL);
    assert(str2!=NULL);
    while(*str1!='\0')//让指针指向str1的最后面
    {
        str1++;
    }
    while(*str1++ = *str2++)
    {
        ;
    }
    return ret;

}

test section

int main()
{
    char arr1[10]="hello ";
    char arr2[]="word";
    my_strcat(arr1,arr2);
    printf("%s",arr1);
    system("pause");
    return 0;

}

operation result
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325757918&siteId=291194637