C ++ common string processing functions and Examples

Source: Internet
Author: User
Tags strtok
Common C ++ string processing functions and examples-general Linux technology-Linux programming and kernel information. For more information, see the following. Char * strcpy (char * s1, const char * s2)
Copy string s2 to string array s1 and return the value of s1.

Char * strncpy (char * s1, const char * s2, size_t n)
Copy up to n characters in string s2 to string array s1 and return the value of s1.

Char * strcat (char * s1, const char * s2)
Add string s2 to the end of string s1. The first character of s2 redefines the null terminator of s1. Returns the value of s1.

Char * strncat (char * s1, const char * s2, size_t n)
Add up to n characters in string s2 to the end of string s1. The first character of s2 redefines the null terminator of s1. Returns the value of s1.

Int strcmp (const char * s1, const char * s2)
Compares string s1 and string s2. When s1 is equal to, less than, or greater than s2, the function returns 0, less than 0, or greater than 0.

Int strncmp (const char * s1, const char * s2, size_t n)
Compares n characters in string s1 with string s2. When s1 is equal to, less than, or greater than s2, the function returns 0, less than 0, or greater than 0.

Char * strtok (char * s1, const char * s2)
Use a series of strtok calls to tag the s1 string (the string is divided into logical components, just like every word in a line of text) and separate it with the characters contained in string s2. The first call contains s1 as the first parameter, and the subsequent call continues to mark the same string, including NULL as the first parameter. The current mark pointer is returned for each call. If no more tags are available during function calling, NULL is returned.

Size_t strlen (const char * s)
Returns the number of characters before the null Terminator.

Example:

// The source code is compiled in the Visual c ++ 6.0 environment

# Include

# Include

Int main ()

{

Char str1 [50] = "Happy birthday to", str2 [] = "coffeehu ";

Char temp1 [2, 100], temp2 [6], * temp;

Char str [] = "This is a sentence with 7 tokens ";

Strcpy (temp1, str1 );

Strncpy (temp2, str1, 5 );

Temp2 [5] = '\ 0 ';

Cout <"strcpy result:" <
Cout <"strncpy result:" <temp2 <"\ n ";

Cout <"strcat result:" <strcat (str1, str2) <"\ n ";

Cout <"strncat result:" <strncat (str1, str2, 6) <"\ n ";

Cout <"strcmp result:" <strcmp (temp2, "Happ") <"\ n ";

Cout <"strncmp result:" <strncmp (str1, "Happy", 5) <"\ n ";

// Strtok function eg.

Temp = strtok (str ,"");

While (temp! = NULL)

{

Cout <temp <'\ n ';

Temp = strtok (NULL ,"");

}

Cout <"strlen result:" <strlen (str2) <"\ n ";

Return 0;

}

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.