String array functions provided in C + +

Source: Internet
Author: User
Tags function prototype strcmp

The C + + language provides programmers with a number of functions that make it very easy to handle character arrays. These functions allow you to copy, concatenate, compare, and find the character array.

When an array is declared, C + + automatically produces a pointer to the first data element of the array. When you use the name of an array, you are actually using a pointer to the array.


Here are some of the string array functions provided in C + +, their prototypes and invocation statements, and the input of these functions is a character pointer (char*).


function prototype void strcat (char *dest, char *src);

Function: Concatenate the string src to the string dest behind.

Example:

Char s1[30]= "I love C + +";

Char s2[10]= "very much!";

strcat (S1,S2);

The data in S1 is "I love C + + very much"


function prototype void strcpy (char *dest, char *src);

Function: Copy the string src to the dest.

Example:

Char s1[30];

Char s2[30]= "I love C + +";

strcpy (S1,S2);

The data in S1 is "I love C + +"


function prototype int strcmp (char *s1,char *s2);

Function: Compares S1 and S2, returns 0 if equal, or returns a positive value if s1s2. Often in the if and while statements, or to sort words (alphabetically)

Example:

Char s1[15]= "Apple";

Char s2[15]= "Banana"

if (strcmp (S1, "Apple")) ==0; Check if S1 is Apple

if (strcmp (S1,S2) <0);//But S1 returns true before S2 in the alphabet


Function prototype char *strstr (char*,char*);

Function: Returns a pointer to the first occurrence of S2 in S1, or null if S1 does not contain S2

Example:

Char s1[50]= "The rain in Spain was mainly on the plan.";

Char s2[10]= "Elephant";

Char s3[10]= "Rain";


Char *char_ptr;


Char_ptr=strstr (S1,S2); Because no elephant,char_ptr is null in S1

Char_ptr=strstr (S1,S3); Char_ptr is the address in memory for R


function prototype int strlen (char*)

Function: Returns the length of the string, not including the null character

Example:

int length;

Char s1[25]= "I love c++!";

Length=strlen (S1); Length of 11


Function prototype Char *strncpy (char *dest,char *src,int N)

Function: Copy characters from SRC to dest

Example:

Char s1[10]= "abcdef"

strncpy (S1, "ABC", 3); The value of S1 is "ABCdef"


String array functions provided in C + +

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.