String (String) in C / C ++
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:
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
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 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:
According to Tutorialspoint
Previous post: Array (Array) in C / C ++
Next article: Cursor in C / C ++
You should read it
May be interested
- Cursor in C / C ++pointer - pointer in c / c ++ language is easy to learn and interesting. some tasks in the c / c ++ language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a pointer.
- Cursor NULL in C / C ++it is always a good practice to assign a null pointer to a pointer variable in case you don't know the exact address to be assigned. this is done at the time of variable declaration. a pointer that is assigned null is called a null pointer.
- Arithmetic pointer in C / C ++as explained in the main chapter, the cursor in c / c ++ is an address, which is a numeric value. therefore, you can perform arithmetic operations on a pointer as you would with numeric values. there are 4 arithmetic operators that can be used on pointers: ++, -, +, and -.
- Cursor and Array in C ++pointers and arrays are closely related. in fact, pointers and arrays are interchangeable in some cases.
- Array of pointers in C ++before we understand the concept of pointer arrays, we consider the following example, which uses an array of 3 integer numbers.
- Pointers to pointers in C ++a pointer to a cursor is an unidirected form or a string of pointers. typically, a pointer contains the address of a variable.