[C] strcat of library function

Concatenate strings

  Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

  destination andsource shall not overlap.

  The above content is the introduction of the strcat function on the C++ official website. It can be seen that the strcat function connects the content of the source string to the back of the target string, and the terminating null character in the target string is overwritten by the first character of the source string. The end of the string contains a null character. It is important to note that the target string cannot overlap with the source string .

  Next, the source code that implements the strcat function is given:

#define _CRT_SECURE_NO_WARNINGS 1

/*
* Copyright (c) 2018, code farmer from sust
* All rights reserved.
*
* File name: MyStrcat.c
* Function: Implement the library function strcat
*       char * strcat ( char * destination, const char * source )
*
* Current version: V1.0
* Author: sustzc
* Completion date: April 20, 2018 11:38:57
*/

# include <stdio.h>
# include <assert.h>

/*
* Function name: MyStrcat
*
* Function: connect the content of the source string to the back of the target string,
* the terminating null character in the destination string is overwritten by the first character of the source string,
* The end of the new string contains a null character.
*
* Entry parameters: dest, src
*
* Export parameter: dest
*
* return type: char *
*/

char * MyStrcat (char * dest, const char * src)
{
	char *cp = dest;

	assert((NULL != src) && (NULL != cp));
		
	while ('\0' != *cp)
	{
		cp++;
	}
	while (*cp++ = *src++)
	{
		;
	}

	return dest;
}

int main(void)
{
	char str1[10] = "abc";
	char *str2 = "123";

	printf("str1: %s\n", str1);
	printf("str2: %s\n", str2);
	printf("connect after,str1: %s\n", MyStrcat(str1, str2));

	return 0;
}

output result


Guess you like

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