May 11, 2021 C++
There are two types of string representations available in C?
C-style strings originate in the C language and continue to be supported in C. T he string is actually an array of one-dimensional characters terminated with the null character ''. Therefore, a string that ends with null contains the characters that make up the string.
The following declaration and initialization create a "Hello" string. B ecause empty characters are stored at the end of the array, the character array is one more size than the number of characters in the word "Hello". '};
Depending on the array initialization rules, you can write the above statement as follows:
char greeting[] = "Hello";
Here is the memory representing the string defined in C/C?
In fact, you don't need to put the null character at the end of the string constant. W hen you initialize an array, the compiler automatically places '' at the end of the string. Let's try to output the string above
#include <iostream>
using namespace std;
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Greeting message: Hello
There are a large number of functions in C++to manipulate strings ending in null: supports a wide range of functions that manipulate null-terminated strings:
Serial number | Function & purpose |
---|---|
1 |
strcpy(s1, s2);
Copy the string S2 to the string S1. |
2 |
strcat(s1, s2);
Connect the string S2 to the end of the string S1. |
3 |
strlen(s1);
Returns the length of the string S1. |
4 |
strcmp(s1, s2);
如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。 |
5 |
strchr(s1, ch);
Returns a pointer, pointing to the first appearance of the character chi in the string S1. |
6 |
strstr(s1, s2);
Returns a pointer to a position where the character string S2 in the string S1 is returned. |
The following example uses some of the above functions:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[11] = "Hello";
char str2[11] = "World";
char str3[11];
int len ;
// 复制 str1 到 str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// 连接 str1 和 str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// 连接后,str1 的总长度
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
The C++ standard library provides a string class type that supports all of the above operations, plus additional features. Now that we're going to learn about this class in the standard library, let's take a look at the following example:
You may not yet be able to fully understand this example, because so far we haven't discussed classes and objects. So now you can just take a cursive look at the instance and wait until you understand the object-oriented concept before you look back to understand it.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
// 复制 str1 到 str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// 连接 str1 和 str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// 连接后,str3 的总长度
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10