C ++ (string processing function)

Source: Internet
Author: User

 

# Include <iostream. h>
# Include <string. h>
Void main (void)
{
Char str1 [10] = {"Tsinghua "};
Char str2 [10] = {"computer "};
Cout <strncpy (str1, str2, 3) <Endl;
}

Running result: comnghua

Note: The first numchars character in the string source will overwrite the first numchars character in the string destination!

Strcat (char target [], const char source []);

Strcat: connect the source string to the end of the target string.
Strcat Function Application Example
Prototype: strcat (char target [], const char source []);
Function: connect the source string to the end of the target string.
Routine:

# Include <iostream. h>
# Include <string. h>
Void main (void)
{
Char str1 [] = {"Tsinghua "};
Char str2 [] = {"computer "};
Cout <strcpy (str1, str2) <Endl;
}

Running result: Tsinghua computer

Note: The length of character array 2 should be considered when defining the length of character array 1, because the length of the new string after connection is the sum of the two strings. After the string is connected, the terminator of string 1 is automatically removed, and a terminator after the new string is retained at the end of the string.

Strncat (char target [], const char source [], int numchars );

Strncat: receives the first numchars of the string source after the string target.
Example of strncat function application:
Prototype: strncat (char target [], const char source [], int numchars );
Function: receives the first numchars character of the string source to the end of the string target.
Routine:

# Include <iostream. h>
# Include <string. h>
Void main (void)
{
Char str1 [] = {"Tsinghua "};
Char str2 [] = {"computer "};
Cout <strncat (str1, str2, 3) <Endl;
}

Running result: Tsinghua com

Int strcmp (const char firststring [], const char secondstring );

Strcmp: Compares two strings: firststring and secondstring.
Strcmp Function Application Example
Prototype: int strcmp (const char firststring [], const char secondstring );
Function: Compares two strings: firststring and secondstring.
Routine:

# Include <iostream. h>
# Include <string. h>
Void main (void)
{
Char buf1 [] = "AAA ";
Char buf2 [] = "BBB ";
Char buf3 [] = "CCC ";
Int PTR;
PTR = strcmp (buf2, buf1 );
If (PTR> 0)
Cout <"Buffer 2 is greater than buffer 1" <Endl;
Else
Cout <"Buffer 2 is less than buffer 1" <Endl;
PTR = strcmp (buf2, buf3 );
If (PTR> 0)
Cout <"Buffer 2 is greater than buffer 3" <Endl;
Else
Cout <"Buffer 2 is less than buffer 3" <Endl;
}

The running result is: Buffer 2 is less than buffer 1.
Buffer 2 is greater than buffer 3

Strlen (const char string []);

Strlen: count the number of characters in string.
Strlen Function Application Example
Prototype: strlen (const char string []);
Function: counts the number of characters in a string.
Routine:

# Include <iostream. h>
# Include <string. h>
Void main (void)
{
Char STR [100];
Cout <"enter a string :";
Cin> STR;
Cout <"the length of the string is:" <strlen (STR) <"" <Endl;
}

Running result the length of the string is X (X indicates the total number of characters you entered)

Note: The strlen function is used to calculate the actual length of a string, not including '/0. In addition, the strlen function can also directly test the length of a String constant, such as strlen ("welcome ").

 

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.