String is one of the C ++ standard libraries, which encapsulates string operations.
There are three methods to convert string to char:
1. Data
For example:
String STR = "ABC ";
Char * P = Str. Data ();
2. c_str
For example, string STR = "gdfd ";
Char * P = Str. c_str ();
3. Copy
For example
String STR = "hello ";
Char P [40];
Str. Copy (p, 5, 0); // here, 5 represents the number of characters to be copied, and 0 represents the copy position.
* (P + 5) = '\ 0'; // You must manually add the Terminator.
Cout <P;
Example program:
# Include <iostream>
# Include <string>
Using namespace STD;
Int main ()
{
String line;
Getline (CIN, line );
Char STR [20];
Int size = line. Size ();
Line. Copy (STR, line. Size (), 0 );
// Char STR [20] = "liuyanbo ";
Char upper [20];
For (INT I = 0; I <size; ++ I)
{
// Cout <static_cast <char> (toupper (STR [I]);
Upper [I] = static_cast <char> (toupper (STR [I]);
}
Upper [I] = '\ 0 ';
Cout <upper <Endl;
Return 0;
}