Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the string function strcat

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report