String (String) in C / C ++

This form of string originates from C language and continues to be supported in C / C ++. The string in the C programming language is essentially a one-dimensional array of characters that ends with a null character ''.

C ++ provides two types of string representations as follows:

Chain in the style of C language (C-style),

String class (String) is introduced in standard C / C ++.

Chain style C

This form of string originates from C language and continues to be supported in C / C ++. The string in the C programming language is essentially a one-dimensional array of characters that ends with a null character '' .

The declaration and initialization section below creates a string that includes a word "Hello". To keep null values ​​at the end of the array, the size of the array of characters includes a more string than the number of characters in the "Hello" keyword.

 char loiChao [ 6 ] = { 'H' , 'e' , 'l' , 'l' , 'o' , '' }; 

If you follow the rules for initializing strings, you can write the following command:

 char loiChao [] = "Hello" ; 

Here is the memory cell representation for the above string segment in C / C ++ language:

String (String) in C / C ++ Picture 1

In fact, you do not put the null character at the last position of the constant variable. The C compiler automatically adds '' at the last location of the string when it initializes the string. Try the example to print the following string:

 #include using namespace std ; int main () { char loiChao [ 6 ] = { 'H' , 'e' , 'l' , 'l' , 'o' , '' }; cout << "When we get together, we say:" ; cout << loiChao << endl ; return 0 ; } 

When the above code is compiled and executed, the print result will look like this:

 When we meet , we say : Hello 

The C / C ++ language supports multiple functions to manipulate the ending strings to be null:

STTHàm & Purpose1 strcpy (s1, s2);

Copy the string s2 for the string s1.

2 strcat (s1, s2);

Connect string s2 at the end of string s1.

3 strlen (s1);

Returns the length of the string s1.

4 strcmp (s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1 s2.

5 strchr (s1, ch);

Returns the pointer to the first position of ch in s1.

6 strstr (s1, s2);

Returns the pointer to the first position of the string s2 in the string s1.

Here is an example for using some of the above functions:

 #include #include using namespace std ; int main () { char chuoi1 [ 10 ] = "Hello" ; char chuoi2 [ 10 ] = "Christmas" ; char chuoi3 [ 10 ]; int wool ; // copy the copy1 in the first 3 strcpy ( chuoi3 , chuoi1 ); cout << "strcpy (chuoi3, chuoi1):" << chuoi3 << endl ; // Follow two cycles: cycle 1 and cycle 2 strcat ( chuoi1 , chuoi2 ); cout << "strcat (chuoi1, chuoi2):" << chuoi1 << endl ; / / After a long time, one after having a connection with the wool = strlen ( chuoi1 ); cout << "Use of strlen hamstring (chuoi1) for long life 1:" << << << endl ; return 0 ; } 

Running the above C / C ++ program will produce the following results:

String (String) in C / C ++ Picture 2

String class in C / C ++

The C / C ++ standard library provides a type of String class that supports all the above-mentioned string-related operations, and adds many more features. We will learn this class in C / C ++ Standard Library (C ++ Standard Library), but for now, we consider the following example:

At this point, you may not understand this example, because we have not discussed Class and Object in C / C ++. Therefore, you observe and remember them until you understand the concepts of Object Orientation presented in later chapters.

 #include #include using namespace std ; int main () { string chuoi1 = "Hello" ; chuoi2 = "Christmas" ; string chuoi3 ; int wool ; // Why do I copy 1 in the third stage3 = the first 1 ; cout << "Flying gio chuoi3 la:" << chuoi3 << endl ; // Say two stages: cycle 1 and cycle 2 = standard 1 + chuoi2 ; cout << "chuoi1 + chuoi2 has gone through:" << chuoi3 << endl ; / / Due to the chaos of the third party after the first time with the new model = 3 . size (); cout << "Longevity due to the problem3.size ():" << << << endl ; return 0 ; } 

Running the above C / C ++ program will produce the following results:

String (String) in C / C ++ Picture 3

According to Tutorialspoint

Previous post: Array (Array) in C / C ++

Next article: Cursor in C / C ++

4 ★ | 1 Vote | 👨 150 Views
« PREV POST
NEXT POST »