The implementation method of strcat in C language

Source: Internet
Author: User
Tags first string

Recently saw a topic requirements, their code to implement the STRCAT function, so that their own implementation of one of the following:

/** 12. Write a function join and let it implement the string join operation function. */#include <stdio.h>#include <assert.h>#include <stdlib.h>#include <string.h>//============== "self-fulfilling strcat" ==============Char* Join (Char* STR1,Const Char* str2) {assert (str1! = NULL && STR2! = null);Char* Pstr = str1; while(*pstr++); --PSTR; while((*pstr++ = *str2++)! =0);returnSTR1;}intMain () {#define N    CharBuf[n] ="Hello";Char* STR2 ="world!";CharBuf1[n] ="Hello";Char* res = join (buf, str2);Char* Res1 =strcat(Buf1, STR2);printf("%s\n", res);printf("%s\n", res1);return 0;}

Because, we know that the first string of the function input needs to have enough memory space, if the space is not enough, it will cause the stack crash.

So we tried to look at the standard library, the following is the standard STRCAT.C implementation code

/****strcat.c-contains strcat () and strcpy ()**       Copyright (c) Microsoft Corporation. All rights reserved.**purpose:*       Strcpy () copies one string onto another.**       Strcat () concatenates (appends) a copy of the source string to the*       End of the destination string, returning the destination string.********************************************************************************/#include <cruntime.h>#include <string.h>#ifndef _mbscat#pragma function (strcat,strcpy)#endif/* _mbscat * //****char *strcat (DST, SRC)-concatenate (append) one string to another**purpose:*       Concatenates src onto the end of Dest. Assumes enough*       Space in Dest.**entry:*       Char *dst-string to which "src" are to be appended*       const char *src-string to is appended to the end of "DST"**exit:*       The address of "DST"**exceptions:********************************************************************************/char * __cdecl strcat (char * DST,const char * src        ){char * cp = DST;While (*CP)cp++; /* Find end of DST * /while (*cp++ = *src++); /* Copy src to end of DST * /return (DST); /* RETURN DST * /}/****char *strcpy (DST, src)-copy one string over another**purpose:*       Copies the string src into the spot specified by*       Dest Assumes enough.**entry:*       char * dst-string over which "src" are to be copied*       const char * src-string to being copied over "DST"**exit:*       The address of "DST"**exceptions:*******************************************************************************/char* __cdecl strcpy (char *DST, const char * src) {char * cp = DST;While (*cp++ = *src++)                ; /* Copy src over DST * /return (DST);}

It is clear that the original Strcat function does not examine the various problems that may arise. In other words, if we enter a null pointer, the program will crash directly <-_->!!
No wonder a strcat_s version is now available.

/****strcat_s.c-contains Strcat_S ()**   Copyright (c) Microsoft Corporation. All rights reserved.**purpose:*   strcat_s () concatenates (appends) a copy of the source string to the*   End of the destination string.********************************************************************************/#include <string.h>#include <internal_securecrt.h>#define _func_prologue#define _FUNC_NAME strcat_s#define _CHAR CHAR#define _dest _DST#define _size _sizeinbytes#define _SRC _SRC#include <tcscat_s.inl>

The contents of TCSCAT_S.INL are as follows

/****tcscat_s.inl-general Implementation of _tcscpy_s** Copyright (c) Microsoft Corporation. All rights reserved.**purpose:* This file contains the general algorithm for strcat_s and its variants.*****/_func_prologueerrno_t __cdecl _func_name (_char *_dest, size_t _size,Const_char *_src) {_char *p; size_t available;/ * Validation section * /_validate_string (_dest, _size);    _validate_pointer_reset_string (_SRC, _dest, _size);    p = _dest; Available = _size; while(Available >0&& *p! =0) {p++;    available--; }if(Available = =0) {_reset_string (_dest, _size);    _return_dest_not_null_terminated (_dest, _size); } while((*p++ = *_src++)! =0&&--available >0)    {    }if(Available = =0) {_reset_string (_dest, _size);    _return_buffer_too_small (_dest, _size); } _fill_string (_dest, _size, _size-available +1); _return_no_error;}

It is obvious that strcat_s, in addition to adding a parameter, adds the ability to detect the data, and to detect the validity of it, as well as the rollback operation.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to implement Strcat in the

C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.