Strcpy,strcmp,strlen,strcat function prototype in C language

Source: Internet
Author: User
Tags assert strcmp

//strcat (DEST,SRC) adds the string src refers to at the end of the Dest (overwrites the ' + ' at the end of the dest) and adds ' % 'Char*strcat (Char* Strdest,Const Char*strsrc) {    Char*res=strdest; Assert ((strdest!=null) && (strsrc!=NULL));  while(*strdest) strdest++;  while(*strdest=*strsrc) {strdest++; STRSRC++; }    returnRes;}//strcpy (DEST,SRC) copies a string with a null terminator starting from the SRC address to an address space starting with destChar*strcpy (Char*strdest,Const Char*strsrc) {    Char*res=strdest; Assert ((strdest!=null) && (strsrc!=NULL));  while(*strdest=*strsrc) {strdest++; STRSRC++; }    returnRes;}

Today to Evans Innovation interview, The Examiner asked me a simple implementation, that is: the implementation of their own writing STRCPM, IBM has also written strcpy prototype, these functions in the interview is often tested, very representative, and suddenly was asked to be really a bit unprepared. Now write down for everyone to study and later review: (the following procedure by myself)

1. The Strcat function prototype is as follows:
Char *strcat (char *strdest, const char *STRSCR)//Adds the source string to const, indicating that it is an input parameter
{
char * address = strdest; If the statement is placed after the Assert, the compilation error
ASSERT ((strdest! = null) && (STRSCR! = null)); Add a non-0 assertion to the source address and destination address
while (*strdest)//is a simplified form of while (*strdest!= '/0 ')
{//if using while (*strdest++), an error occurs because + + is not subject to looping
strdest++; Constrained. So be in the loop body + +; Because if *strdest last point
}//To the end flag of the string '/0 '.
while (*strdest++ = *strscr++)
{
NULL; The cycle condition can be used in + +,
}//Here you can add the statement *strdest= '/0 '; Is there any need?
return address; To implement chained operations, return the destination address
}
The following is an example of debugging in VC6.0, where the function name is replaced with Strcata.
#include <stdio.h>
#include <assert.h>
Char *strcata (char *strdest,const char *strscr)
{
char * address = strdest;
ASSERT ((strdest! = null) && (STRSCR! = null));
while (*strdest)
{
strdest++;
}
while (*strdest++ = *strscr++)
{
NULL;
}
return address;
}

void Main ()
{
Char str1[100]={"I Love"};
Char str2[50]={"China"};
printf ("%s/n", Strcata (STR1,STR2));
}
2. The strcpy function prototype is as follows:
Char *strcpy (char *strdest, const char *STRSCR)
{
Char *address=strdest;
ASSERT ((strdest! = null) && (STRSCR! = null));
while (*STRSCR)//is a simplified form of while (*strscr! = '/0 ');
{
*strdest++ = *strscr++;
}
*strdest = '/0 '; When the STRSCR string length is less than the original strdest string length
return address; , if you do not change the statement, you will get an error.
}
The following is an example of debugging in VC6.0, where the function name is replaced with Strcpya.
#include <stdio.h>
#include <assert.h>
Char *strcpya (char *strdest, const char *STRSCR)
{
char *address = strdest;
ASSERT ((strdest! = null) && (STRSCR! = null));
while (*STRSCR)
{
*strdest++ = *strscr++;
}
*strdest = '/0 ';
return address;
}

void Main ()
{
Char str1[100]={"I Love"};
Char str2[50]={"China"};
printf ("%s/n", Strcpya (STR1,STR2));
}
3. The STRCMP function prototype is as follows:
int strcmp (const char *str1,const char *STR2)
{
int len = 0;
ASSERT ((str1! = '/0 ') && (str2! = '/0 '));
while (*str1 && *str2 && (*str1 = = *STR2))
{
str1++;
str2++;
}
return *STR1-*STR2;
}
The following is an example of debugging in VC6.0, where the function name is replaced with Strcmpa.
#include <stdio.h>
#include <assert.h>
int Strcmpa (const char *str1,const char *STR2)
{
int len = 0;
ASSERT ((str1! = '/0 ') && (str2! = '/0 '));
while (*str1 && *str2 && (*STR1==*STR2))
{
str1++;
str2++;
}
return *STR1-*STR2;
}

void Main ()
{
Char str1[100] = {"I Love"};
Char str2[50] = {"China"};
printf ("%d/n", Strcmpa (STR1,STR2));
}
4. The Strlen function prototype is as follows:
int strlen (const char *STR)
{
int len = 0;
ASSERT (str = NULL);
while (*str++)
{
len++;
}
return Len;
}
The following is an example of debugging in VC6.0, where the function name is replaced with Strlena.
#include <stdio.h>
#include <assert.h>
int Strlena (const char *STR)
{
int len = 0;
ASSERT (str = NULL);
while (*str++)
{
len++;
}
return Len;
}
void Main ()
{
Char str1[100] = {"I Love"};
Char str2[50] = {"China"};
printf ("%d/n", Strlena (STR1));
}

Strcpy,strcmp,strlen,strcat function prototype in C language

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.