Collation of common string functions in C language

Source: Internet
Author: User
Tags strcmp strlen strtok


Function: strcat
Prototype:

Char * strcat (char * dest, char * src );

Function: add the src string to the end of dest (overwrite '\ 0' at the end of dest) and add' \ 0 '.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings. Returns the pointer to dest.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char str [100] = {0 };
Char * str1 = "Hello", * str2 = "", * str3 = "World! ";
Strcat (str, str1 );
Strcat (str, str2 );
Strcat (str, str3 );
Printf ("% s \ n", str );
Return 0;
}
Function: strcpy
Prototype:

Char * strcpy (char * dest, char * src );

Function: copies the string ending with NULL indicated by src to the array indicated by dest.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings. Returns the pointer to dest.


# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char dest [20];
Char * src = "Hello World! ";
Strcpy (dest, src );
Printf ("% s", dest );
Return 0;
}

Function: strncpy
Prototype:

Char * strncpy (char * dest, char * src, int n );

Function: copy the first n bytes of the string ending with NULL in src to the array indicated by dest.
Note: If the first n bytes of src do not contain NULL characters, the result will not end with NULL characters. If the src length is less than n bytes, fill the dest with NULL until the n bytes are completely copied. The memory areas specified by src and dest cannot overlap and dest must have enough space to hold src strings. Returns the pointer to dest.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char dest [20];
Char * src = "Hello World! ";
Strncpy (dest, src, 5 );
Dest [5] = '\ 0 ';
Printf ("% s", dest );
Return 0;
}

Function: strncat
Prototype:


Char * strncat (char * dest, char * src, int n );

Function: add the first n characters of the string referred to by src to the end of dest (overwrite '\ 0' at the end of dest) and add' \ 0 '.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings. Returns the pointer to dest.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char str [20] = "Hello ";
Char * str1 = "World !!! ";
Strncat (str, str1, 6 );
Printf ("% s \ n", str );
Return 0;
}
Function: strchr
Prototype:

Char * strchr (char * s, char c );

Function: locate the first occurrence of character c in string s.
Note: return the pointer at the first occurrence of c. If c does not exist in s, return NULL.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str = "Hello World! ";
Char * p, c = 'w ';
P = strchr (str, c );
If (p ){
Printf ("% s", p );
} Else {
Printf ("Not Found ");
    }
Return 0;
}
Function: strrchr
Prototype:

Char * strrchr (char * s, char c );

Function: finds the position of the character c at the end of string s.

Note: return the pointer at the position where c finally appears. If c does not exist in s, return NULL.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str = "Hello World! ";
Char * p, c = 'O ';
P = strrchr (str, c );
If (p ){
Printf ("% s", p );
} Else {
Printf ("Not Found ");
    }
Return 0;
}

Function: strstr
Prototype:

Char * strstr (char * haystack, char * needle );

Function: find the first position of the needle from the string haystack (not compare the Terminator NULL ).
Note: The pointer pointing to the needle position for the first time is returned. If no pointer is found, NULL is returned.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str = "Hello World! ";
Char * p, * n = "or ";
P = strstr (str, n );
If (p ){
Printf ("% s", p );
} Else {
Printf ("Not Found ");
    }
Return 0;
}
Function: strcmp
Prototype:

Int strcmp (char * s1, char * s2 );

Function: compares s1 and s2 strings.
Description: when s1s2 is used, the return value is greater than 0.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str1 = "Hello World! ";
Char * str2 = "hello world! ";
    
Printf ("% d", strcmp (str1, str2 ));
    
Return 0;
}

Function: strncmp

Prototype:

Int strcmp (char * s1, char * s2, int n );

Function: compares the first n characters of string s1 and s2.
Description: when s1s2 is used, the return value is greater than 0.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str1 = "Hello World! ";
Char * str2 = "Hello world! ";
    
Printf ("% d", strncmp (str1, str2, 7 ));
    
Return 0;
}

Function: strlen

Prototype:


Int strlen (char * s );

Function: calculates the length of string s.
Description: returns the length of s, excluding the Terminator.


# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str = "Hello World! ";
    
Printf ("% d", strlen (str ));
    
Return 0;
}

Function: strtok
Prototype:


Char * strtok (char * s, char * delim );

Function: splits a string into a group of tag strings. S is the string to be decomposed, and delim is the separator string.
Note: For the first call, s must point to the string to be decomposed, and then the call should set s to NULL. Strtok searches for characters contained in delim in s and replaces them with NULL ('\ 0') until the entire string is searched. Returns the string pointing to the next tag. If no string is marked, NULL is returned.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char s [20] = "Hello World! ";
Char * d = "o ";
Char * p;
P = strtok (s, d );
While (p ){
Printf ("% s \ n", p );
P = strtok (NULL, d );
    }
    
Return 0;
}

Function: memset
Prototype:


Void * memset (void * buffer, int c, int count );

Function: sets the first count byte of the memory area referred to by buffer to character c.
Note: the pointer to the buffer is returned.


# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char s [20] = "Hello World! ";
Memset (s, 'O', 10 );
Printf ("% s", s );
Return 0;
}

Function: memcpy

Prototype:


Void * memcpy (void * dest, void * src, unsigned int count );

Function: Copy count bytes from the memory area indicated by src to the memory area indicated by dest.
Note: The memory areas specified by src and dest cannot overlap. The function returns a pointer to dest.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * s = "Hello World! ";
Char d [20];
Memcpy (d, s, strlen (s ));
Printf ("% s", d );
Return 0;
}

Function: memmove

Prototype:


Void * memmove (void * dest, const void * src, unsigned int count );

Function: Copy count bytes from the memory area indicated by src to the memory area indicated by dest.
Note: src and dest indicate that the memory areas can overlap, but the src content will be changed after replication. The function returns a pointer to dest.


# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char str [20] = "Hello World! X ";
Memmove (str, str + 6, 7 );
Printf ("% s", str );
Return 0;
}

Function: memcmp

Prototype:


Int memcmp (void * buf1, void * buf2, unsigned int count );

Function: Compare the first count bytes of buf1 and buf2 in the memory area.
Description: when buf1buf2 is enabled, the return value is greater than 0.

# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str1 = "Hello World! ";
Char * str2 = "Hello world! ";
    
Printf ("% d", memcmp (str1, str2, strlen (str1 )));
    
Return 0;
}

Function: memchr
Prototype:

Void * memchr (void * buf, char ch, unsigned count );

Function: searches for the character ch from the first count bytes of the memory area specified by buf.
Note: When the first ch character is encountered, stop searching. If the call succeeds, a pointer to the ch character is returned; otherwise, NULL is returned.


# Include <stdio. h>
# Include <string. h>
Int main (int argc, char ** argv)
{
Char * str = "Hello World! ";
Char * p, c = 'w ';
P = (char *) memchr (str, c, strlen (str ));
If (p ){
Printf ("% s", p );
} Else {
Printf ("Not Found ");
    }
Return 0;
}
-- EOF --

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.