#include
#include
using namespace std;
int main()
{
char str[100] = "i love yanghe"; //c语言中用字符数组表示字符串
//string类型提供了很多字符串的操作方法 所以还是string类型使用比较方便
string s1;
s1 = "i love china";
string s2 = " i love you ";
s2 = s1;
string s3("i love wagnle dwdw");
//用c语言的字符数值初始化c++string字符类对象
string s4(str);
cout << s4[0]<< endl;
cout << s1<< endl;
cout << sizeof(s1) << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s2 << endl;
cout << sizeof(s2) << endl;
cout << s2.size() << endl;
cout << s2.length() << endl;
cout << s3 << endl;
cout << sizeof(s3) << endl;
cout << s3.size() << endl;
cout << s3.length() << endl;
//s[n] 返回字符串中第n个字符
//动态分配数组长度 这个是c语言所不具备的
int num = 6;
string s5(num, 'h');
//cout << s5 << endl;
const char *p = s5.c_str();
//s.c_str这个就是返回一个字符串s中的内容指针(指向字符串的常量指针)????? 主要是将c++中字符串转化为c语言中的字符串样式
//cout << s5.c_str()<< endl;
//cout << p<< endl;
char strp[100];
strcpy_s(strp, sizeof(strp), p);
cout << strp << endl;
string s6 = "joiajfoi" + string("fjoajoi");
string s7 = "fjefjoaewi" + s6;
cout << s6 << endl;
cout<< s7 << endl;
//范围for语句使用 就是把字符一个一个打出来(中间用回车分割,类似于装置)
for( char c:s7)
{
cout << c << endl;
}
cout << s7 << endl;
for (char &c : s7)
{
c = toupper(c);
cout << c << endl;
}
cout << s7 << endl;
system("pause");
return 0;
}