すでに文字型はマスターしたと思います。
次に、文字列型を使ってみます。
C++のプログラム中では、文字列は、```string```型の変数として扱います。
変数は、今で学んだ変数と同様に命名ルールを守っていれば好きな名前を設定できます。
```string```型の初期化には、文字列リテラルが使われます。
つまり、以下の様に初期化されます。
string str = "string literal";
プログラミングで使う記号たち!
#include <iostream>
#include <string>
using namespace std;
int main()
{
//char moji=11;//文字1文字を記憶できる型
//char moji2 = 'u';//文字リテラル:文字に見えるけど番号を代入してる
//cout << moji << endl;
//cout << moji2 << endl;
//for文でアスキーコードの文字の部分
//32~126まで?を表示してみよう
char a0 = 'h';
char a1 = 'e';
char a2 = 'l';
char a3 = 'l';
char a4 = 'o';
char a5 = '!';
//"hello!" 文字列リテラル
string str = "hello!";
string str2 = "world!";
//coutはすげーやつなので、大体のC++の型を<<で流してやると
//その方に合わせた表示をしてくれる(ように作られている)
cout << a0 << a1 << a2 << a3 << a4 << a5 << endl;
cout << "hello!" << endl;
str = str + str2;
cout << str << endl;
//0から数えた4文字目(5文字目)を取り出す
cout << str[4] << endl;
//for文でhello!world!を縦に表示
//str[0][1][2][3][4][5][6][7][8][9][10][11]
// 'h''e''l''l''o''!''w''o''r''l' 'd' '!'
//str.size()で文字数を取得できる!
//この文字列の中で、!がそれぞれ何文字目かを表示!
for (int i = 0; i < str.size(); i++)
{
//cout << str[i] << endl;
if (str[i] == '!')
{
cout << "!は" << i << "文字目です" << endl;
}
}
string str3 = "hello!world!";
if (str == str3)
{
cout << str <<"と" << str3 << "は同じ文字列" << endl;
}
else {
cout << str << "と" << str3 << "は別の文字列" << endl;
}
/*for (char i = 32; i < 127; i++)
{
cout << i;
if ((i + 1) % 16 == 0)
cout << endl;
}*/
return 0;
}