The difference between C string and C++ string

  • 2020-04-02 01:44:14
  • OfStack

In C++, the string is encapsulated as a data type string, which can directly declare variables and perform string operations such as assignment. Here's the difference between a C string and a C++ string:

  C string
  String object (C++)

The name of the required header file
  < The string > or < String. H >
  < The string > or < String. H >

Why you need a header file
  To use string functions
  To use the string class

A declarative way
  Char name [20].
  String name;

Initialization mode
  Char name [20] = "nihao";  
  String name = "nihao";

Must you declare the string length?
  is
  no

Use a null character?
  is
  no

The implementation of string assignment
  Strcpy (name, "John");
  Name = "John";

advantages
  faster
  Easier to use, preferred solution

Can you assign a longer string than an existing character?
  Can't
  can

C++ often USES string functions
Char s1[]="I am a student";
Char s2 [20] = "the teacher";
Char s3 [] = "student";
Int result;
Char s4 [20], * p;

(1) string length int strlen(char * STR)
cout < < Strlen (s1) < < Endl; The output of 14
cout < < Strlen (s2) < < Endl; The output of 7

Char *strcpy(char *str1,char *str2)
Strcpy (s4, s2);     / / s4 to "the teacher"

Char *strcat(char *str1,char*str2)
Strcat (s2, s3); / / s2 for "teacherstudent"

(4) string comparison int STRCMP (char *str1,char * STR) // comparison is the corresponding character of the ASCII code value, if str1 > Str2, returns 1
Result = STRCMP (s2, s3);     / / result > 0
Result = STRCMP (s2, s2);     / / result = 0
Result = STRCMP (s3, s2);     / / result < 0

Char * STRCHR (char * STR,char ch)
P = STRCHR (s1, 's');       // finds the position of the return character in the string, otherwise -1 is returned
Strcpy (p, s2);           //s1 is "I am a teacher"

(6) find whether there is a substring equal to another string in one string

(7) intercept the substring to form a new string

Input to a string
(1) method 1:
Use the input operator to populate a C string variable
Such as:
Char a, [80].
cin > > A;
Note: when a C string is read in this way, the initial whitespace characters (Spaces, tabs, and newlines) are ignored, and the input stops at the next space or newline.

(2) method 2: Get the entire line of input (including Spaces) using the predefined function getline
The getline function takes two arguments: the first argument is the C string variable that receives the input; The second parameter specifies the maximum number of characters that getline can accept.

Such as:
Char a, [80].
Cin. Getline (a, 80);
When a line ends, the input stops.

Input to the C++ string class
(1) method 1: the same as method 1 of C string input.
(2) method 2: use the getline function.

Such as:
String a;
Getline (cin, a);

Conversion between a string object and a C string
You can store C strings in variables of type string, for example:
Char a [] = "nihao";
String b;
B = a;
However, string object cannot be automatically converted to C string, so it needs to be explicitly cast, and c_str() of string class is needed.

Such as:
Strcpy (a, biggest _str ());

String to number conversion
The atoi function takes a C string argument and returns the corresponding int value. If the argument does not correspond to an int value, atoi returns 0. The atoi function is in the cstdlib library. If the number is too large to be converted to an int, you can use atol to convert the string to a value of type long.

Such as:
The atoi (" 1234 ");     // returns the integer 1234
The atoi (" # 123 ");     / / returns 0


Related articles: