Table of Contents
String (String) in C // C ++: String (String) in C // C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
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 ; }
Frequently Asked Questions
What should you know about 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 ''.
What should you know about 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.
What is String (String) in C // C ++: 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 ++.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.