Implementation of String functions (first wave)

Source: Internet
Author: User
Tags assert strlen
1. strcpy string Copy

First, let's look at the approximate model of functions in the MSDN Library.

The function of the strcpy function is a string copy,
We can use the pointer to move the assignment to achieve
Until ' I ' stop.

According to function function, we can write the following code

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
Char *my_strcpy ( Char *dest, const char *src)
{
    char *cp = dest;
    ASSERT (Dest!=null),//assertion, dest If the empty program will crash
    while (*dest++ = *src++)//So write in the exit loop at the same time, the ' I ' also assigned to *dest
    {
        null;// Empty statements are written in such a way that it is easier to see
    } return
    CP;
int main ()
{
    char str[5] = "";
    Char *p = my_strcpy (str, "ABCD");
    printf ("%s", p);
    System ("pause");
    return 0;
}

Let's take a look at the source code of the library function and find that it's almost indistinguishable from what we write.

strcpy:
1./*
2. * Strcpy-copy a%nul terminated string
3. * @dest: Where to Copy the string to
4. * @src : Where to copy
the string from 5. *
/6.char *strcpy (char *dest, const char *src)
7.{
8.    char *tmp = dest;
9.    while ((*dest++ = *src++)!= ' ");
One.    return tmp;
13.}

Observe the source code you will find that the strcpy function does not determine whether to cross the steps, so when using this function
Be aware of the string length and the destination string space size. 2. strncpy function

Because the strcpy of the string copy function is not safe, there is a strncpy
What's it for?
function string copy, but more than strcpy one parameter is the number of copies
So we can just make a little change in our judgment.
First look at the MSDN

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
Char *my_strncpy ( Char *dest, const char *src,int count)//const allows code to have better robustness
{
    char *cp = dest;
    ASSERT (Dest!=null);
    while (count--&& (*dest++=*src++))
        {
        NULL;
    }
    if (Count!= 0)
    {
        *dest = ' i ';
    }
    return CP;
}
int main ()
{
    char str[5] = "";
    Char *p = my_strncpy (str, "ABCD", 2);
    Char *p = my_strncpy (str, "ABCD", 2);
    printf ("%s\n", p);
    System ("pause");
    return 0;
}

Now let's look at the source code in the library.

char * __cdecl strncpy (
        char * dest,
        const char * source,
        size_t count
        )
{
        char *start = dest;
  while (Count && (*dest++ = *source++))/    * Copy string/
                count--;

        if (count)                              * pad out with zeroes *
                /while (--count)
                        *dest++ = ';

        return (start);

No difference in nature . 3. Strcat Connection String

String append function

Because it's the APPEND function, it's OK to find the ' yes ' of the target string first and then assign the pointer offset.

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
Char *my_strcat ( Char *dest, const char *src)
{
    char *tem = dest;
    ASSERT (dest!= null&&src!= NULL);
    while (*dest)
    {
        dest++
    }
    while (*dest++ = *src++)
    {
        NULL;
    }
    return tem;
}
int main ()
{
    char str[10] = "ABCD";
    Char *p = my_strcat (str, "ABCD");
    printf ("%s\n", p);
    System ("pause");
    return 0;
}

Strcat Source code:

char * __cdecl strcat (
        char * DST,
        const char * src
        )
{
        char * cp = DST;

        while (*CP)
                cp++;                   /* Find end of DST *

        /while (*cp++ = *src++);       /* Copy src to end of DST * *

        (DST);                  /* Return DST */

}
4. strncat function

MSDN:

Similar to strcat, just add an additional number on the basis of strcat

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
Char *my_strncat ( Char *dest, const char *src,int count)
{
    char *tem = dest;
    ASSERT (dest!= null&&src!= NULL);
    while (*dest)
    {
        dest++
    }
    while (count--&& (*dest++ = *src++))
    {
        NULL;
    }
    return tem;
}
int main ()
{
    char str[10] = "ABCD";
    Char *p = my_strncat (str, "ABCD", 1);
    printf ("%s\n", p);
    System ("pause");
    return 0;
}

The following is the library function source code:

char * __cdecl strncat (
        char * front,
        const char * back,
        size_t count
        )
{
        char *start = front;
  while (*front++)
                ;
        front--;

        while (count--)
                if (!) ( *front++ = *back++)) return
                        (start);

        *front = ' n ';
        return (start);
5. strlen String Length

Msdn:

Or use the pointer to move to achieve, the pointer to move until encountered ' the ', and then a simple counter can find the length of the string.

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
int My_strlen ( const char *str)
{
    int count = 0;
    ASSERT (str!= NULL);
    while (*str++)
    {
        count++
    }
    return count;
}
int main ()
{
    char *str = NULL;
    int t = My_strlen (str);
    printf ("%d\n", t);
    System ("pause");
    return 0;
}

Strlen Library function Source code:

size_t __cdecl strlen (
        const char * str
        )
{
        const char *eos = str;

        while (*eos++);

        return (eos-str-1);
}

You can see that strlen returns a value of siez_t type, which is defined in the header file Stddef.h, is an unsigned integer type so
if ((strlen (x)-strlen (y)) >=0) Such an expression is incorrect because strlen returns an unsigned type value, so the left side of the >= is a value of an unsigned type, and unsigned values cannot be negative, so here the conditions are constant.
Here you can see the library function does not use a counter to one plus, but directly with the ' address ' addresses minus the address to the first element minus 1
In C, the same type of pointer to the address can be subtracted, the number of elements in the middle of +1

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.