strcpy, strncpy, strcmp, strncmp, strlen source

Source: Internet
Author: User

strcpy
#include <string.h>
#include <assert.h>
char * strcpy (char *strdest, const char *STRSRC)//Adds the source string to const, indicating that it is an input parameter
{
ASSERT ((strdest! = null) && (STRSRC! = null)); Check the validity of pointers by adding a non-0 assertion to the source address and destination address
char *str = strdest;
while ((*strdest++ = * strsrc++)! = ');//The end of the loop body, the end of the strdest string is correctly added '
return str; To implement chained operations, return the destination address
}

strncpy

#include <string.h>
char * strncpy (char * dest,const char * source,int count)
{
char *start = dest;
while (Count && *dest++ =*source++)/*/copy String */
count--;
if (count)/* pad out with zeroes */
while (--count)
*dest++ = ' + ';
return (start);
}

strcmp

#include <string.h>
int strcmp (const char *str1,const char *STR2)
{
/* Do not use while (*str1++==*str2++) to compare, when not equal will still execute once + +,
The comparison value returned by return is actually the next character. The + + should be placed in the loop body. */
while (*str1 = = *STR2)
{
if (*str1 = = ' + ')
Return0;
str1++;
str2++;
}
return *STR1-*str2;
}

strncmp
Case insensitive
int strncmp (const char * s1, const char * s2, size_t len)
{
while (len--) {
if (*s1 = = 0 | | *s1! = *S2)
return *S1-*s2;
s1++;
s2++;
}
return 0;
}
Case sensitive
#include <ctype.h>
int strnicmp (const char * s1, const char * s2, size_t len)
{
Register signed Char R;
while (len--) {
if (r = ToUpper (*S1)-ToUpper (*s2)) | | *s1 = = 0)
return R;
s1++;
s2++;
}
return 0;
}

Strlen

size_t strlen_a (const char *str) {
size_t length = 0;
while (*str++)
++length;
return length;
}
size_t strlen_b (const char *str) {
const char *CP = str;
while (*cp++);
return (CP-STR-1);
}

strcpy, strncpy, strcmp, strncmp, strlen source

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.