Implementation of partial function function

Source: Internet
Author: User
Tags function prototype strcmp

The realization of 1:strcpy

function function: Copy the string starting from the SRC address into a space with dest as the start address,

It contains the ' + ' at the end of the SRC string.

Code implementation:

Implementation of the <stdio.h>//simulation of the #include strcpy
#include <assert.h>
void my_strcpy (char *dest, const char *SRC)/* function does not need to return */
{
ASSERT (dest);
ASSERT (SRC);
while (*SRC)/* Copy the SRC character from front to back to Dest */
{
*DEST=*SRC;
dest++;
src++;
}
*dest= '/*/* copy last ' \ ', because strcpy has the function of copying ' \ ' in src to dest * *


{
Char str1[15];
Char str2[]= "Hello World";
my_strcpy (str1, str2);
printf ("%s\n", str1);
return 0;
}

Operation Result:

650) this.width=650; "title=" 01.png "src=" http://s2.51cto.com/wyfs02/M01/76/C7/wKiom1ZcM-7CL4dPAABeOiDzjY0848.png "alt=" Wkiom1zcm-7cl4dpaabeoidzjy0848.png "/>

The realization of 2:STRCMP

function function: String size comparison, if there are str1,str2 two strings there are two results, equal, str1 large, str2 large.

The first element of the str1 and the first element of the STR2 compare their ASCII values, if the comparison to two characters at the end of the size is returned 0, if there is no comparison to the location of the "\" size is also returned corresponding value. If the current element of the str1 current element is greater than str2 returns an integer, otherwise a negative number is returned.

Code implementation:


#include <stdio.h>//simulation implements strcmp
#include <assert.h>
int my_strcmp (const char *STR1, const   Char *str2)/*STR1 and str2 do not need to change */
{
 assert (str1);
 assert (STR2);
 while (*STR1==*STR2)
 {
  if (*str1== ')/* can indicate that str1 and str2 at this time are compared to the last, and both are '% ' */
  {
   return 0;
  }
  str1++;/* if str1 and str2 are equal but not at the end of the string let str1 and str2 move backwards, compare again */
  str2++;       
 }
    return *str1-*str2;/* If two inequalities are returned with ASCII */
}
int main ()
{
 int ret=0;
 char str1[]= "AAC";
 char str2[]= "abc";
 ret=my_strcmp (STR1,STR2);
 if (ret==0)
 {
  printf ("str1 and str2 equal \ n");
 }
 else if (ret>0)
 {
     printf ("str1 greater than str2\n");
 }
 else if (ret<0)
 {
     printf ("str1 less than str2\n");

}
return 0;
}

Operation Result:

650) this.width=650; "title=" 03.png "src=" http://s1.51cto.com/wyfs02/M00/76/C7/wKiom1ZcOLbRhYLaAABd_1SSdJw699.png "alt=" Wkiom1zcolbrhylaaabd_1ssdjw699.png "/>

3: Improved implementation of the last Strstr,strcat,strncat (all three functions returned are addresses.) Strcat and Strncat are the addresses that return dest, and can be chained access)

Implementation of the <stdio.h>//simulation of the #include strcat
#include <assert.h>
Char *my_strcat (char *dest, const char *SRC)
{
Char *address=dest;
ASSERT (dest);
ASSERT (SRC);
while (*dest)
{
dest++;
}
while (*SRC)
{
*DEST=*SRC;
dest++;
src++;
}
*dest= ' + ';
return address;
}
int main ()
{
Char str1[15]= "Hello";
Char str2 [] = "World";
printf ("%s\n", My_strcat (STR1, str2));
return 0;
}
*/




/*
Implementation of the <stdio.h>//simulation of the #include strncat
#include <assert.h>
Char *my_strncat (char *dest, const char *SRC)
{
int n,i;
Char *address=dest;
scanf ("%d", &n);
while (*dest)
{
dest++;
}
for (i=0;i<n;i++)
{
* (dest+i) =* (src+i);
}
* (dest+i+1) = ' + ';
return address;
}
int main ()
{
Char str1[15]= "Hello";
Char str2 [] = "World";
printf ("%s\n", My_strncat (STR1, str2));
return 0;
}
*/



/*
Implementation of the <stdio.h>//simulation of the #include strstr
#include <string.h>
Char *my_strstr (char *str1,char *str2)
{
int i,j;
int Len1=strlen (STR1);
int Len2=strlen (STR2);
for (i=0;i<len1;i++)
{
for (j=0;j<len2;j++)
{
if (Str1[i]==str2[j])
Return &str1[i];
}
}
return NULL;
}
int main ()
{
Char str1[]= "He is good teacher";
Char str2[]= "good";
printf ("%s\n", My_strstr (STR1,STR2));
return 0;
}
*/

The realization of 4:strlen

function function: Strlen completes a count function, scans the string from the back to the front until it encounters a stop, and the final count does not include '/'.

There are three implementations of code, counter method, address operation method, recursive function method:

*/

Implementation of strlen counter method #include <stdio.h>//simulation

#include <assert.h>

int My_strlen (const char *STR)

{

int count=0;

ASSERT (str);

while (*STR)

{

count++;

str++;

}

return count;

}

int main ()

{

int ret=0;

Char str[]= "ABCDEFG";

Ret=my_strlen (str);

printf ("%d\n", ret);

return 0;

}

Implementation of strlen address arithmetic by #include <stdio.h>//simulation

#include <assert.h>

int My_strlen (const char *STR)

{

Char *tmp=str;

ASSERT (str);

while (*STR)

{

str++;

}

Return str-tmp;/* address subtraction to get the number of elements in the middle */

}

int main ()

{

Char str[]= "ABCDEFG";

printf ("%d\n", My_strlen (str));

return 0;

}

Recursive method for realizing strlen using #include <stdio.h>//simulation

#include <assert.h.>

int My_strlen (const char *STR)

{

ASSERT (str);

if (*str== ')

return (0);

Else

{

Return My_strlen (str+1) +1;

}

}

int main ()

{

int ret=0;

Char str[]= "ABCDEFG";

Ret=my_strlen (str);

printf ("%d\n", ret);

return 0;

}

650) this.width=650; "title=" 04.png "src=" http://s1.51cto.com/wyfs02/M01/76/C7/wKioL1ZcPzrwz-YBAABUSQ1iDoc375.png "alt=" Wkiol1zcpzrwz-ybaabusq1idoc375.png "/>

5:memcpy function

The memory copy function, which copies the starting position of the memory address of SRC as a count byte to the target dest

The space at the point of memory is overwritten from the first position of the dest, and is overwritten by a total of n bytes of information in SRC.

Function prototype: void *memcpy (void *dest, const void *src,int count)

Code implementation:


Implementation of the <stdio.h>//simulation of the #include momcpy
#include <assert.h>
void* my_momcpy (void *buf1,const void *buf2, int count)/*/null type pointer, so BUF1,BUF2 can receive arrays of any type */
{
Char *ret=buf1;/* to * return the address of dest for output */
Char *dest= (char*) buf1;/* Converts the pointer coercion type to a character type so that it can be copied one byte at a-
Char *src = (char*) buf2;
ASSERT (BUF1);
ASSERT (BUF2);
while (count--)
{
*DEST=*SRC;
dest++;
src++;
}
return ret;
}
int main ()
{
Char str[]= "Hello World";
Char arr[10];

printf ("%s\n", my_momcpy (arr, str, 6));/* Copy 6 bytes of information into ARR[10] * *
return 0;

}


The difference between memcpy and strcpy:

strcpy can only operate on strings, and in memory there are 0 o'clock strcpy may mistakenly think of as ' ", end the copy work, which will cause the limitations of the copy operation; memcpy is a memory copy function that can copy any type of array, It is a copy of one byte of information from SRC to dest, or it can be copied across types. (The ASI standard does not stipulate that memcpy implement memory overlapping copies, but some compilers implement this functionality.)

6:memmove function

function function: Copy the Count bytes information to the memory area of SRC to dest, can realize the copy work of the memory overlap, also can realize the copy work in the memory overlapping place

Function prototypes: void *memmove (void *dest, const void *src, Int count)

Code implementation:


Implementation of the <stdio.h>//simulation of the #include mommove
#include <assert.h>
void* my_mommove (void *buf1,const void *buf2, int count)/* A pointer to an empty type receives the array name so that the type coercion of all possibilities is converted to char* at once */
{
Char *ret=buf1;
Char *dest= (char*) buf1;/* forces the type to be converted to char* so that the information can be copied one byte at a byte */
Char *src = (char*) buf2;
ASSERT (BUF1);
ASSERT (BUF2);
if ((dest< (Src+count)) &AMP;&AMP;DEST&GT;SRC)/* Memory Heavy stack */
{
while (count--)
{
* (Dest+count) =* (Src+count);
}
}
else/* memory does not overlap */
{
while (count--)
{
*DEST=*SRC;
dest++;
src++;
}
}
return ret;
}
int main ()
{
int arr[10]={1,2,3,4,5,6,7,8,9,0};
int i;
My_mommove (arr+5,arr+2,16);/* Copy 3 4 5 6 to 6 7 8 9 position, 4 elements 16 bytes */
for (i=0;i<10;i++)
{
printf ("%d", arr[i]);
}
return 0;
}
Operation Result:

650) this.width=650; "title=" Picture 1.png "src=" http://s3.51cto.com/wyfs02/M00/76/C8/ Wkiol1zcsufrowmuaaashobazp8004.png "alt=" Wkiol1zcsufrowmuaaashobazp8004.png "/>

(This time realized the Strcmp,strcpy,strlen,memcpy,memmove, but also improved the Strstr,strcat,strncat)


This article from "Anser" blog, reproduced please contact the author!

Implementation of partial function 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.