C Language:: Simulation implements STRCAT function

Source: Internet
Author: User
Tags assert function prototype

Topic Requirements

Write a C-language program simulation to implement the STRCAT function.

(We might as well first simulate the implementation of the STRCAT function, and then against the String.h library function strcat function code implementation, shoulder to side with the master.)

Algorithm analysis

strcat function function: Concatenate two strings, and finally return the first address of the concatenated string.

strcat function prototype: Char *strcat (char *dest,const char *src);

After we have clearly understood the function and prototype of the STRCAT function, it is easy to analyze the algorithm ...

Algorithm Summary

The first step is to loop through the contents of the Dest to the section of '/'.

Step two: Copy the content that SRC points to the dest, including the ' + ' after src.

Core code
The simulation implements the Strcat function Char *my_strcat1 (char *dest,const char *src) {char * ret = dest;//Docking Two pointers are asserted assert (dest); assert (SRC); /dest Traversal to \0while (*dest) {dest++;} Copy src content after dest while (*src) {*dest++ = *src++;} Append \0*dest = ' + ' after dest, return ret;}
Full Test code
/** This code by the high minor original, the copyright High Minor Blog All * Author: High Minor * Date: 2016-8-7* code function: Simulation Implementation STRCAT function * Integrated development environment: Microsoft Visual Studio */#include < stdio.h> #include <stdlib.h> #include <assert.h>//simulation implementation Strcatchar *MY_STRCAT1 (char *dest,const char *src {char * ret = dest;//docking receives two pointers for assert assert (dest), assert (SRC),//to traverse dest to \0while (*dest) {dest++;} Copy src content after dest while (*src) {*dest++ = *src++;} Append \0*dest = ' + ' after dest, return ret;} Implement method in library function char *my_strcat2 (char *dest,const char *src) {char * CP = dest;//dest traversal to \0while (*CP) {cp++;} Copy src content after dest while (*cp++=*src++) {;} return dest;} int main () {char str1[20] = "Hello"; char str2[] = "Gxd blog!"; Char *ret = MY_STRCAT1 (STR1,STR2);p rintf ("%s\n", ret); System ("pause"); return 0;}
Output

      List of functions
    1. printf () function functions, prototypes, usages, and instances
    2. ASSERT () macro features, prototypes, usages, and instances
    3. Strcat () functions, prototypes, usages and examples

I wish you all the best in a minor.

This article by high minor blog original!

Original address: http://gaoxiaodiao.com/p/21.html

If you want to reprint, please specify the source!

PS: I was in the sophomore semester, in order to earn yoga instructor study fees, weekend in KFC part-time job, life rhythm super fast.

2 in the afternoon walk from school, 3:30 to work, until 12 o ' clock. The school is closed, take a taxi, go to the Internet bar to bag the night.

(KFC can reimburse part of the fare.)

At that time I thought that if after graduating from college, every day is like this life, this future is too dark!

During that time, I always thought that this should be the darkest time of the university!

Looking back now, get up 6 o'clock in the morning every day and go to the Yoga Hall.

5 o'clock in the afternoon after class, rushed to the bus station, because there are 6 points of programming class, the Night of 9 o'clock class ...

Although now more than a part-time job, but I enjoy ...

This is perhaps the legendary growth of it!

C Language:: Simulation implements STRCAT function

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.