C string functions

Source: Internet
Author: User

String functions in C Language

There are two main categories: Copy, comparison, and others.

Copied:

1. strcpy

Function prototype

Char * strcpy (char * strdestination, const char * strsource );

Function

Copy the content of strsource to strdestination, including '\ 0'

Return Value

Returns the content of strdestination.

Features

The memory regions of strsource and strdestination may overlap and are not defined

Check whether the strdestination size is sufficient

 

2 strcpy_s

Function prototype

 
errno_t strcpy_s(   char *strDestination,   size_t numberOfElements,   const char *strSource );

Function

Copy the content of strsource to strdestination, including '\ 0'

Return Value

0 is returned successfully.

Features

The memory regions of strsource and strdestination may overlap and are not defined

Check the strdestination size

 

3 memset

Function prototype

Void * memset (void * Dest, IntC, Size_tCount);

Function

Assign the Count C value to the dest, which is often used to clear the string.

Return Value

Dest

 

4 memcpy

Function prototype

Void*Memcpy (void * Dest, Const void *SRC, Size_tCount);

Function

Copy count characters from SRC to dest

Return Value

Dest

Features

The SRC and DEST memory overlaps cannot be processed (overlapping regions)

Code prototype

char* m_memcpy(char* dest, const char* src, int n){   char* p=dest;while (n)   {   if (*src!='\0')      *p=*src;  src++;  p++;  n--;   }   *p='\0';   return dest;}

 

 

5 memmove

Function prototype

Void * memmove (void * Dest, Const void *SRC, Size_tCount);

Function

Copy count characters from SRC to dest

Return Value

Dest

Features

Overlapping regions)

The processing method is to determine whether there are overlapping parts. If yes, copy them from the back to the front.

Code prototype

Char * m_memmove (char * DEST, const char * SRC, int N) {char * P = DEST; If (SRC + n> P & SRC <p) // overlapped for (INT I = 1; I <n + 1; I ++) * (p + n-I) = * (SRC + n-I ); else // no overlaps {While (n) {* P = * SRC; SRC ++; P ++; n -- ;}} return DEST ;}

 

String comparison functions

1 strcmp

Function prototype

Int strcmp (const char*String1,Const char*String2);

Features

Case Insensitive

2 strncmp

Function prototype

Int strncmp (const char*String1,Const char*String2,Size_t
Count );

Features

Case Insensitive

 

3 memcmp

Function prototype

Int memcmp (const void * Buf1, Const void *Buf2, Size_tCount);

Features

Similar to strncmp, but further research is needed on the specific differences.

Int r = strncmp ("1234", "1234", 7); // the string is 0 and exits. The loop is 4 times.
Int r2 = memcmp ("1234", "1234", 7); // exit when the loop is 7 times or an inequality occurs. Therefore, when the number of comparisons is greater than the string length, it is not safe.

It can also be understood that strncmp is a string comparison, so it can be determined by \ 0, but void * is hard to judge.

 

4 strnicmp

Function prototype

 
int strnicmp(   const char *string1,   const char *string2,   size_t count );

Features

Case-insensitive comparison

 

 

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.