C and c ++ strings

Source: Internet
Author: User

C and c ++ strings
Name: strcpy

Function: copy a string.

char* strcpy(char* dest, const char* src){    if(dest == NULL || src == NULL)    {        return NULL;    }    char* tmp = dest;    while((*tmp++ = *src++) != '\0');    return dest;}
Name: strncpy

Function: copy a string.

Strncpy copies the first n characters of the string that src points to ending with '\ 0' to the array indicated by dest, and returns the pointer to dest.

Note:

When n> = sizeof (src), the copy is correct and '\ 0' is added to the end of the dest string ';

When n

char* strncpy(char* dest, const char* src, size_t count){    if(dest == NULL || src == NULL)    {        return NULL;    }    char* tmp = dest;    while(count--)    {        *dest++ = *src++;    }    return tmp;}
Name: strcmp

Function: Compares strings.

int strcmp(const char* s0, const char* s1){    int ret = 0;    unsigned char c1,c2;    while(1)    {        c1 = *s0++;        c2 = *s1++;        if(c1 != c2)        {            return c1 > c2 ? 1 : -1;        }        if(!c1)        {            break;        }    }    return ret;}
Name: strncmp

Function: Compares strings.

int strncmp(const char* s0, const char* s1, size_t count){    int ret = 0;    unsigned char c1,c2;    while(count)    {        c1 = *s0++;        c2 = *s1++;        if(c1 != c2)        {            return c1 > c2 ? 1 : -1;        }        if(!c1)        {            break;        }        count--;    }    return ret;}
Name: reserve

Function: String Inversion

void reserve(char* word){    if(word != NULL)    {        char* start = word;        char* end = word;        while(*end++);        end -= 1;        while(start < end)        {            char s = *start;            *start = *end;            *end = s;            start++;            end--;        }    }}
Name: reserveWords

Function: String Inversion

I love you -----> you love I

void reserveWords(){    char words[] = "I love You";    reserve(words);        char* start = words;    char* end = words;    while(*end != '\0')    {        if(*end == '\0' || *end == ' ')        {            char tmp[20];            memcpy(tmp, start, end - start);            reserve(tmp);            start = end;        }        else if(*end == ' ')        {            start++;            end++;        }        else        {            end++;        }    }}

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.