在C語言中使用數組來存儲字符串,C++提供了String 類擴張C++庫。要使用String類必須在程序中包含#include<string>
用string對象和字符數組之間的區別在於,可以將string聲明成一個簡單的變量,而不用像數組那樣指定大小。
#include<iostrema>
#include<stream>
int main()
{
using namespace std;
string firstname;//存儲字符串
string lastname;
cout << "Enter your first name :";
cin >> firstname;
cout << "Enter your last name :";
cin >> lastname;
cout << "game over" << endl;
getchar();
return 0;
}
string類常用的其他操作
(1)函數strcpy():用來將字符串複製到字符數組中
strcpy(charr1, charr2); //copy charr2 to charr1
(2)函數strcat():將字符串附加到字符數組末尾
strcat(charr1, charr2); //append contents of charr2 to charr1