23-24C++ (46)–String string+ getline(cin, s)+a.length()+a.substr(x, y)+a.find(b)+a.rfind(b)

1. Characters and strings

1. Character: a single character enclosed in single quotes,

Such as ‘a’, ‘_’, the corresponding data type is char.

2. String: any number of characters enclosed in double quotes,

Such as “abc”, “a”, “”, ” “, the corresponding data type is const char [], which can be stored in the string type or in char in the array. It is more convenient to use string type.

Note:

Characters and strings are different types and cannot be assigned to each other using the assignment sign (=).

For example: char a; string b; where variable a is character type and variable b is string type. The types between the two are different, and there cannot be operations similar to b=a or a=b.

2. String string

1. Header file:

To use the string type, you must add a line #include

below #include

2. Basic operations of strings

(1) Define variables

string a; //The value of a is the empty string””

(2) Initialize when defining variables

string a=”ABC”; //At this time the value of a is”ABC”

You can also use string a(“ABC”);

(3) Assign values to already defined variables

string a; a=”ABC”; //At this time the value of a is”ABC”

(4) Input string

string a; cin>>a; //The value of a is determined by the input

Note: cin can only input a string without spaces, ‘\t’, ‘\\
‘, that is, with spaces, ‘\t’, ‘\\
‘ serves as the end marker for string input.

For example: when cin>>a, if the keyboard input ab cde, where there is a space between b and c, then the value of a is “ab”.

(5) Enter a string that may contain spaces (and ‘\t’)

string a; getline(cin, s); //Assign a whole line of input to a

Description:

getline input uses ‘\\
‘ as the end tag of the string input.

For example:

If the keyboard inputs ab cde when gettingline(cin, s), then the value of a is “ab cde”.

(6) Assign the value of one string to another

string a=”ABC”; string b; b=a; //The value of b is “ABC”

(7) Output string

string a=”ABC”; cout<

(8) Output a certain character in the string

string a=”ABC”; cout<

(9) Assign a character in the string to a char type variable

string a=”ABC”; char b; b=a[0]; //The value of b at this time is ‘A’

(10) Modify a certain character value in the string

string a=”ABC”; a[1]=’C’; //At this time the value of a is”ACC”

(11)String length

Definition: The number of characters in a string. For example, the length of “ABC123” is 6. The length of string a: a.length()

[Example 1] Input a string and output the three characters whose subscripts are 0, 2, and 3. Do not separate spaces or newlines.

For example

When ABCDE is entered, ACD should be output.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin >> a;
cout << a[0] << a[2] << a[3];
return 0;
}

【Example 2】Input a string and output the length of the string.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin >> a;
cout << a.length();
return 0;
}

The running results are as follows–

[Example 3] Input two strings and output the larger one.

Comparison rules: Compare the characters with subscript 0 first. If the ASCII codes are different, the size has been determined; otherwise, continue to compare the characters with subscript 1,… until the size is found. For example, “ABC” is larger than “AAA”, “ABC” is smaller than “abc”, “ABCD” is larger than “ABC”, “ABC” and “ABC\ “equal.

You can use >, =, <=, != in the code to compare string sizes.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
if (a > b)
{
cout << a;
}
else
{
cout << b;
}
return 0;
}

【Example 4】Input a string and output the uppercase characters.

For example, if you enter AB53CdAAf, ABCAA should be output.

You can access a certain character in the string a according to the subscript. The legal range of the subscript is from 0 to a.length()-1. For example, string a=”ABCDE”, because the length of string a is 5, each character from a[0] to a[4] corresponds to one character.

Loop statements can be used in the code. The loop argument loops from 0 to length -1, and i is used as a subscript in the loop.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.length(); i + + )
{
if (a[i] >= 'A' & amp; & amp; a[i] <= 'Z')
{
cout << a[i];
}
}
return 0;
}

[Example 5] String concatenation You can use “+” to concatenate strings with strings to return a longer string.

Strings can also be concatenated with characters, and characters can be concatenated with strings.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a = "abcd";
string b = "efg";
char c = 'h';
string s = a + b; //abcdefg
string t = b + c; //efgh
string u = c + b; //hefg
string v = a + b + c; //abcdefgh
return 0;
}

【Example 6】Get substring

A small section of a string is called a substring. Use the substr function to perform substring operations. For example, string a=”ABCDEFG”,

Then a.substr(x, y) returns a string, which means starting from the character with subscript x in a, taking a string of y consecutive characters;

a.substr(z) returns a string, representing the string starting from the character a with subscript z and ending with the end.

So a.substr(3, 2) returns “DE”, and a.substr(3) returns “DEFG”.

【Example 7】Find the position of a substring. Search whether a string is a substring of another string.

If yes, return the subscript of the first occurrence; if not, return -1.

string a = "abcdefgh";
string b = "cde";
int c = a.find(b); //2 means b is found at the 2nd character of a

string a = "abcabcabc";
string b = "acb";
int c = a.find(b); //-1, indicating not found

string a = "abcabcabc";
string b = "abc";
int c = a.rfind(b); //6, rfind searches forward starting from the last character of a

string a = "abcabcabc";
string b = "abc";
int c = a.find(b, 2); //3, start searching from the character with subscript 2 in a

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 56918 people are learning the system