C++ strcat string connection library function method

  • 2020-04-02 01:02:41
  • OfStack

The prototype
Extern char *strcat(char *dest,char * SRC);
usage
# include < String. H >
function
Add the SRC reference string to the end of dest (overwriting the '\0' at the end of dest) and add '\0'.
instructions
The memory area indicated by SRC and dest must not overlap and dest must have enough space to hold the SRC string.
Returns a pointer to dest.
For example,
  Char str4[] = "Hello world";
  Char str5[] = "Hello World";
  cout < < Strcat (str4 str5) < < Endl;
It's going to go wrong because str4 doesn't have enough space
The following is an implementation of my own, shortcomings, also hope to correct!!

#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
//Concatenation string
char* mystrcat(char* destStr,const char* srcStr)  //What if two strings are the same string?
{
 assert(destStr != NULL && srcStr != NULL);
 char* temp=destStr;
 while(*destStr != '0')
 {
  ++destStr;
 }
 while(*destStr++ = *srcStr++)
  NULL;
 return temp; //To implement the chain operation, the destination address is returned
}
int _tmain(int argc, _TCHAR* argv[])
{
 char str1[25] = "Hello world";
 char str2[] = "Hello World";
 cout << mystrcat(str1,str2) << endl;
 return 0;
}


Related articles: