C + + Common string processing functions and usage examples

Source: Internet
Author: User
Tags strcmp strlen strtok

Char *strcpy (char *s1, const char *S2)

Copies the string s2 to the string array S1, returning the value of S1

Char *strncpy (char *s1, const char *S2, size_t N)

Copies the maximum n characters in the string S2 to the string array S1, returning the value of S1

Char *strcat (char *s1, const char *S2)

Adds a string S2 to the back of the string S1. S2 The first character of the S1 to redefine the null terminator. Returns the value of the S1

Char *strncat (char *s1, const char *S2, size_t N)

Adds up to n characters in the string S2 to the back of the string S1. S2 The first character of the S1 to redefine the null terminator. Returns the value of the S1

int strcmp (const char *S1, const char *S2)

Compares string s1 and string s2. function returns a value of 0, less than 0, or greater than 0 when S1 equals, less than, or greater than S2

int strncmp (const char *S1, const char *S2, size_t N)

Compares n characters and string S2 in a string S1. function returns a value of 0, less than 0, or greater than 0 when S1 equals, less than, or greater than S2

char * strtok (char *s1,const char *s2)

The S1 string is marked with a series of strtok calls (dividing the string into logical components, like each word in a line of text), separated by the characters contained in the string S2. The first invocation contains S1 as the first argument, which continues to mark the same string when called, and contains null as the first argument. Returns the current tag pointer each time it is invoked. Returns NULL if the function call no longer has more tags

size_t strlen (const char *s)

Determines the length of the string, returning the number of characters before the null terminator

Use examples:

Source code compiled in visual c++6.0 environment
#include <iostream.h>
#include <string.h>
int main ()
{
Char str1[50] = "Happy birthday to", str2[] = "Coffeehu";
Char temp1[100],temp2[6], * temp;
Char str[] = "This is a sentence with 7 tokens";
strcpy (Temp1, str1);
strncpy (Temp2, STR1, 5);
TEMP2[5] = ' the ';
cout << "strcpy result:" <<temp1 << "\ n";
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.