C + + string

Source: Internet
Author: User

C-style string

C-style strings originate from the C language and continue to be supported in C + +. The string is actually an array of one-dimensional characters terminated with the null character '% '. Therefore, a null-terminated string that contains the characters that make up the string.

The following declaration and initialization creates a "Hello" string. Because empty characters are stored at the end of the array, the size of the character array is one more than the number of characters in the word "Hello". ‘

Char greeting[6] = {'H'e'L  'l''o'  '};

Based on the array initialization rules, you can write the above statement as follows:

Char " Hello ";

The following is a memory representation of a string defined in C/s + +:

In fact, you do not need to put a null character at the end of a string constant. The C + + compiler automatically places ' \ ' at the end of the string when initializing an array. Let's try outputting the above string:

Instance:

#include <iostream>using namespacestd;intMain () {Chargreeting[6] = {'H','e','L','L','o',' /'}; cout<<"Greeting Message:"; cout<< Greeting <<Endl; return 0;}

When the above code is compiled and executed, it produces the following results:

Greeting Message:hello

There are a number of functions in C + + to manipulate null-terminated strings: supports a wide range of functions that manipulate null-terminated strings:

Serial Number Functions & Purposes
1 strcpy (s1, S2);
Copy the String s2 to the string S1.
2 strcat (s1, S2);
The connection string S2 to the end of the string S1.
3 Strlen (S1);
Returns the length of the string S1.
4 strcmp (s1, S2);
If S1 and S2 are the same, then 0 is returned, if S1<S2 the return value is less than 0, if s1>s2 the return value is greater than 0.
5 STRCHR (s1, ch);
Returns a pointer to the position of the first occurrence of the character ch in the string S1.
6 Strstr (s1, S2);
Returns a pointer to the position of the first occurrence of the string S2 in the string s1.

The following example uses some of the above functions:

#include <iostream>#include<cstring>using namespacestd;intMain () {Charstr1[ One] ="Hello"; Charstr2[ One] =" World"; Charstr3[ One]; intLen; //copy str1 to STR3strcpy (STR3, str1); cout<<"strcpy (STR3, str1):"<< STR3 <<Endl; //connecting str1 and str2strcat (str1, str2); cout<<"strcat (str1, str2):"<< str1 <<Endl; //The total length of the str1 after the connectionLen =strlen (STR1); cout<<"strlen (str1):"<< Len <<Endl; return 0;}

When the above code is compiled and executed, it produces the following results:

10

C + + string

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.