On the function prototype of strcpy strcmp strlen strcat in C language

  • 2020-05-17 06:04:50
  • OfStack

Examples are as follows:


//strcat(dest,src) the src The reference string is added to dest End (coverage dest At the end of '\0' ) and add '\0'
char *strcat(char * strDest, const char *strSrc)
{
  char *res=strDest;
  assert((strDest!=NULL)&&(strSrc!=NULL));
  while(*strDest)strDest++;
  while(*strDest=*strSrc)
  {
    strDest++;
    strSrc++;
  }
  return res;
}
//strcpy(dest,src) From the src The address begins and contains null The terminator string is copied to dest The starting address space 
char *strcpy(char *strDest, const char *strSrc)
{
  char *res=strDest;
  assert((strDest!=NULL)&&(strSrc!=NULL));
  while((*strDest=*strSrc)!='\0')
  {
    strDest++;
    strSrc++;
  }
  return res;
}

Today, I went to the interview of creative thinking, the examiner asked me a simple implementation, that is, I wrote the implementation of strcpm, IBM has also been tested to write strcpy prototype, these several functions are often tested in the interview, very representative, suddenly asked a bit caught off guard. Write it down now for you to study and review later :(the following procedure was passed by myself)

1. The prototype of Strcat function is as follows:


char *strcat(char *strDest, const char *strScr) // Adds the source string const , indicating that it is an input parameter 
{
    char * address = strDest;       // If this statement is placed assert After that, the compilation went wrong 
    assert((strDest != NULL) && (strScr != NULL)); // Add a non to the source and destination addresses 0 assertions 
    while(*strDest)       // is while(*strDest!='/0') Reduced form 
    {            // If use while(*strDest++) It's going to go wrong because ++ It's not cycled 
       strDest++;        // Constraints. So it's in the circulation ++ ; Because if *strDest Finally refers to 
    }            // Marks the end of the string '/0' . 
    while(*strDest++ = *strScr++) 
    {
       NULL;         // Can be used in this loop condition ++ . 
    }             // You can add a statement here *strDest='/0'; Is it necessary? 
return address;        // To achieve the chain operation, the destination address is returned 
}

Here is an example of debugging in VC 6.0, with the function name strcata instead.


#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 prototype of Strcpy function is as follows:


char *strcpy(char *strDest, const char *strScr)
{
    char *address=strDest;
    assert((strDest != NULL) && (strScr != NULL));
    while(*strScr)          // is while(*strScr != '/0') A simplified form of; 
    {
       *strDest++ = *strScr++;
    }
    *strDest = '/0';            // when strScr The string length is smaller than the original strDest String length 
    return address;           // If you do not change the statement, you will make an error. 
}

Here is an example of debugging in VC6.0, with the function name strcpya instead.


#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 prototype of Strcmp function 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;
}

Here is an example of debugging in VC6.0, with the function name strcmpa instead.


#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 prototype of Strlen function is as follows:


int strlen(const char *str)
{
  int len = 0;
    assert(str != NULL);
    while(*str++)
    {
       len++;
    }
    return len;
}

Here is an example of debugging in VC6.0, with the function name strlena instead.


#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));
}



Related articles: