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
- How to Manipulate Strings in Javastrings are sequences of characters. for example, 'hello!' is a string because it is made up of the characters 'h', 'e', 'l', 'l', 'o', and '!'. in java, strings are objects, which means that there is a string class that has fields and...
- How to insert characters at the beginning or end of a string in Excelrecently software tips received many questions asking how to insert characters at the beginning or end of a string in excel. so today, dexterity software introduces readers how to insert more characters into the string in this article, invite you to track.
- Write a program to reverse a string in Pythonwhat is string reversal in python? how to use the string reverse function in python? let's find out with tipsmake.com.com!
- The LEFT function, how to use the left-hand string cutting function in Excelthe left function in excel is used to trim the left string of characters in data parameters.
- Make changes in Gitjerry creates a copy of the repository on his machine and decides to perform basic operations. so he created the file string.c. after adding content, string.c will look like s
- Who would have thought that the production process of the familiar familiar string was complicated and such a feata familiar string, items that we still use every day to tie and pack goods. its presence is considered by everyone to be obvious, but most people don't know how the string is produced.
- How to use ConcateNate function on Excelconcatenate function on excel is a function that connects strings of characters in different data columns in a table, forming a complete string of characters.
- Analysis of an attack (Part 2)we have already introduced you in a section of observable information while opening the package string sent by nmap. the sent string starts with an icmp echo response to determine if the computer or network has been assigned to the ip address. add v & agr
- Python data type: string, number, list, tuple, set and dictionaryin this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
- CONCATENATE function (string concatenation function) in Exceluse the concatenate function when you want to concatenate existing text strings into a single, complete text string. for the syntax and usage of the concatenate function, follow the article below.