Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the string function strcat

Shulou Source: shulou.com Published: 2022-06-03 06:29:43 11月16日 Update

This article mainly explains "how to use string function strcat", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use string function strcat" bar!

The string functions in C language are as follows

Get string length

Strlen

String function with unlimited length

Strcpy

Strcat

Strcmp

String function with limited length

Strncpy

Strncat

Strncmp

String lookup

Strstr

Strtok

Error message report

Strerror

String function strcat with unlimited length

The old rule, let's take a look at what the document says, as follows

Strcat document

Char * strcat (char * destination, const char * source)

Concatenate strings

Connection string

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.

Appends a copy of the source string to the destination string. The'\ 0' in the destination string is overridden by the first character of the source string, and then'\ 0' is at the end of the new string.

Destination and source shall not overlap.

Destination string and source string should not overlap

You can know.

The source string must end with'\ 0'.

Although the document does not say that the target space must be large enough, you can still know when you think about it, that is, the target space must be large enough to accommodate the contents of the source string, otherwise it will not be appended.

Realize

It is a good habit to assert that the pointer is not empty.

Char* my_strcat (char* dest, const char* src) {assert (dest! = NULL); assert (src); char* rest = dest; / / 1. Find the'\ 0' while (* dest! ='\ 0') {dest++;} / / 2 of the destination string. Append, which means that the string is copied, as in the previous implementation of strcpy: while (* dest++ = * src++) {;} return rest;} int main () {/ / char arr1 [] = "hello"; / / char arr2 [] = "world"; / append arr2 to arr1 / / strcat (arr1, arr2); / / printf ("% s\ n", arr1) / / error will be reported and there is not enough space. The above words are incorrect / / you can fix a large space for the size of arr1, such as arr1 [30] char arr1 [30] = "hello\ 0xxxxxxxxx"; char arr2 [] = "world"; / / append arr2 to arr1 / / strcat (arr1, arr2); my_strcat (arr1, arr2); printf ("% s\ n", arr1); return 0;}

In general, the idea of implementation is to append the source string to the destination string to achieve string concatenation. The problem is how to find the end of the destination string, which is very simple, just find'\ 0' and append it as soon as you find'\ 0'. Append is to copy the source character directly to the destination space, which is a loop, and the copy ends when you encounter'\ 0'. This completes the string concatenation.

Thank you for reading, the above is the content of "how to use string function strcat", after the study of this article, I believe you have a deeper understanding of how to use string function strcat, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Character string function purpose space that is length content document learning restriction idea copy target or error problem insufficient information writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei Microsoft Shulou Information Redmi