未分類

c++ で文字列を合成(連結)する方法

投稿日:

c言語では、sprintf 関数を使って文字列を合成(連結)することができる。

いっぽう、c++ では、文字列を string で表して「+」演算子で文字列を合成することができる。

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string str1 = "Good ";
string str2 = "morning";

string gousei = str1 + str2;

cout << gousei << endl;

return 0;
}

結果

Good morning

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

tex で脚注を表示する方法

\footnote を使う。 例 \footnote{this is a footnote.} 結果

no image

python3 で文字列のリストを結合して1つの文字列にする方法

リストに対して、join メソッドを実行する。 例 list1 = [‘hello, ‘,’good ‘, ‘morning’] str1 = ”.join(list1) print(str1) 結 …

no image

C言語で文字をアルファベット順にずらす

C言語で文字をずらすには、char 型の変数に1を加えればよい。 例 #include <stdio.h> int main(void) { char c; c = ‘a’; c++; p …

no image

tex で分数がつぶれないようにする

tex で、複数行を書いたときに分数がつぶれないようにする。 displaystyle を使う。 例 first $\frac{1}{1+\sqrt{x}+\sqrt{y}}$ のかわりに、 seco …

no image

c言語で文字をつなげる方法

sprintf 関数を使って、result 変数に文字列 s1 と文字列 s2 を結合した文字列を出力することができる。 例 #include<stdio.h> int main(void) { …