Implementation of strcat functions and strcat Functions

Source: Internet
Author: User

Implementation of strcat functions and strcat Functions
Prototype extern char * strcat (char * dest, char * src );

Usage# Include <string. h>

FunctionAdd the string indicated by src to the end of dest (overwrite '\ 0' at the end of dest) and' \ 0 '. Returns the pointer to dest.

DescriptionThe memory areas specified by src and dest cannot overlap and dest must have enough space to hold src strings..

Example

Char str4 [] = "Hello world ";
Char str5 [] = "Hello World ";
Cout <strcat (str4, str5) <endl;

An error occurs because str4 does not have enough space.

The following is an implementation of my own. I hope to correct it !!!

# Include "stdafx. h "# include <iostream> # include <assert. h> using namespace std; // connection string char * mystrcat (char * destStr, const char * srcStr) // What if the 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 chained operations, return the destination address} int _ tmain (int argc, _ TCHAR * argv []) {char str1 [25] = "Hello world "; char str2 [] = "Hello World"; cout <mystrcat (str1, str2) <endl; return 0 ;}

Related Article

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.