C++文字列の挿入、結合のコードについて

このQ&Aのポイント
  • C++文字列の挿入、結合のコードについて
  • C++のコードを使用して、指定した文字列を挿入したり、結合したりする方法について説明します。
  • また、上記のコード例では、入力を繰り返し処理するためにdo-whileループが使用されています。
回答を見る
  • ベストアンサー

C++文字列の挿入、結合のコードについて

実行結果のような出力をするためには、 以下のコードの(ウ)(エ)(オ)の部分には何を入れたらよいのでしょうか? よろしくお願いします。 #include <iostream> #include <string> using namespace std; int main( ) { string str1="ABCDEF"; string str2="0123"; string str3; string q; do { (ウ) ; cout << str3 << endl; (エ) ; cout << str1 << endl; cout << "quit?"; cin >> q; } while ( (オ) ); cout << "終了" << endl; return 0; } <実行結果(出力結果)> ABCDEF0123 ABC123DEF quit?q ABC123DEF0123 ABC123123DEF quit?qu ABC123123DEF0123 ABC123123123DEF quit?quit 終了

noname#225287
noname#225287

質問者が選んだベストアンサー

  • ベストアンサー
  • f272
  • ベストアンサー率46% (7998/17100)
回答No.1

いろいろな解答が考えられるけど、たとえば ウ str3=str1+str2; エ str1=str1.substr(0,3)+str2.substr(1)+str1.substr(3); オ q!="quit"

関連するQ&A

  • ofstream::getがおかしい!

    ofstream::getの動作がおかしいのです #include <string> #include <iostream> #include <fstream> using namespace std; void main(void) { ifstream ifs; ofstream ofs; string str; char c; str="abc"; cout<<"before str: "<<str<<endl<<endl; ofs.open("gomi");ofs<<str;ofs.close(); ifs.open("gomi");for(str="";ifs.eof()==0;str+=c)ifs.get(c);ifs.close(); cout<<"after str: "<<str<<endl<<endl; } の結果が before str: abc after str: abcc になります 最後のcは何でつくのでしょうか? 回避する方法を教えてください

  • 段落毎に配列に挿入するには?

    C++の基本的な配列でわからないことがあります。 例えば以下のような,3段落があるテキストファイルを用意しておき, -------------- //abc.txt abcde fghij klmno -------------- 用意した配列char data[3][20]へ, 上から順番にdata[0]に"abcde",data[1]に"fghij",data[2]に"klmno"を挿入したいです。 とりあえず1行だけ,以下のようなプログラムで出力表示できました。 #include <string> #include <iostream> using namespace std; int main() { char cir_data[3][20]; string filename("abc.txt"); FILE* fp = fopen( filename.c_str(), "r" ); fgets(data[0], 20, fp); cout << data[0] << endl; fclose(fp); return 0; } 結果は, -------------------- abcde -------------------- となったので,2・3番目もforループでできるかなと思ったら,良い結果が出ません。 以下は間違えているプログラムです。 int main() { char cir_data[3][20]; int i; string filename("abc.txt"); FILE* fp = fopen( filename.c_str(), "r" ); for(i=0; ; ){ fgets(cir_data[i], 20, fp); if(strlen(cir_data[i])<3) break; if(cir_data[i][0] != '/') i++; cout << cir_data[i] << endl; } fclose(fp); cout << "i = " << i << endl; return 0; } 結果は文字化けしてます。 何回ループしたか,iも出力してみましたが,なぜか6回カウントされています。 どこが誤っているのかわからず困っています。

  • C++

    C++で書いた下記のプログラムが、文字の入力が1023個までなら実行されますが、1024個を超えると実行されません。なぜだか分かる方、教えてください。 #include <iostream> #include <string> using namespace std; int main(void) { string Str; cin >> Str; cout << "here"; }

  • wstringの主力

    wstring (または wchar_t)の出力がうまくいかず困っています。 基本的な質問になるかと思いますがよろしくお願いします。 #include <string> #include <wchar.h> using namespace std; int main() {  wstring str = L"test";  wprintf(L"%s \n", str.c_str());//(1)  wprintf(L"%s \n", L"test");//(2)  wprintf(L"test \n");//(3)  // cout << str <<endl; //(4)  cout << str.c_str() <<endl;//(5) } 出力結果 (1) t (2) t (3) test (4) コンパイルエラー (5) 望むのは当然(3)の出力です。 web上で(1)や(4)のような記述はみかけたのですが、(4)に関してはなぜコンパイルエラーになるかもわからず。。 os linux. gcc 4.1.1

  • C/C++関数間でのStringクラスの扱い

    以下のようなコードを実行してみましたが思い通りに動いてくれません. "sample"という文字列がstrへとコピーされると思ったのですが. stringクラスのc_str()メソッドはconst char*だと言っているので無理矢理キャストしたのが原因でしょうか.stringクラスは記憶領域を自動で変更してくれるのではないのですか.それともこの挙動は仕様ですか. -------- 以下コード -------- #include <iostream> #include <string> using namespace std; int func(char *); int main(void) {     string str("");     func((char *)str.c_str());     cout << "String: " << str << endl;     return EXIT_SUCCESS; } int func(char *buf) {     buf = "sample";     return 0; } -------- 以上コード --------

  • Sortプログラムについて

    自分で作成したプログラムなのですが、ソートしたあと、出力ファイルに書き込みたいのですが、書き込めません。 どこが、悪いのかわからないので教えてください。 ところどころに、出力がありますが、それはトレースしたやつなので、それはほっといてくださって構いません。 下にプログラムを載せます。 よろしくお願いします。 #include <iostream> #include <fstream> #include <list> #include <string> using namespace std; int main() { char Str[255]; list<string> str; ifstream in("ttest", ios::binary | ios::in); if (!in){ cout << "入力ファイルが読み込めない" << endl; exit(1); } ofstream out("out", ios::binary | ios::out); if (!out){ cout << "出力ファイルが読み込めない" << endl; exit(1); } while (!in.eof()){ //!!!! in.getline(Str,255); str.push_back(Str); } str.sort(); list<string>::iterator p; p = str.begin(); while (!in.eof()){ cout << *p; out << *p++; } in.close(); out.close(); getchar(); return 0; }

  • VBAの文字列の中に”(全角のダブルコーテーション)を使う

    VBAのStringの中に”(全角のダブルコーテーション)を使おうとしているのですが、VisualBasicEditorにより自動的に ""(半角のダブルコーテーション2つ)に変換されてしまいます。 Dim str as String str = "abc”def"    ↓ str = "abc""def" 文字列中に全角のダブルコーテーションを使う方法を教えてください。よろしくお願いします。

  • boost::formatの値をstring型にコピーしたい

    boost::formatの値をstring型にコピーしたいのですが、うまくいきません。boost自体初めてで以下のサイトからダウンロードし、展開後VS2005のVCのインクルードフォルダーにboostフォルダーをまるまるコピーしただけですが・・・ http://sourceforge.net/project/showfiles.php?group_id=7586 boost 1.34.1 #include <iostream> #include <boost/format.hpp> using namespace std; using boost::format; void main(){ double x = 1.234; string str("abc"); //cout << format("%10.3f, [%16s]") % x % str << endl; // サンプルはこうでした。 // いったんstring型に入れて表示させたい。 string y; y = boost::format("%10.3f, [%16s]") % x % str; // エラー箇所 std::cout << y << std::endl; }

  • C++の無限ループを解決してください

    アルゴリズムを勉強するときに以下のソースを書きました; void weighted_quick_union_algorithm() { static const int volume = 10; enum status { terminate_, union_, find_ }; string str; status sta; vector<int> system(volume, 0); vector<int> size(volume, 1); for (int index = 0; index != volume; ++index) { system[index] = index; } do { cout<<"cin"<<endl; cin >> str; for (string::size_type index = 0; index != str.size(); ++index) str[index] = toupper(str[index]); if (str == "UNION") sta = union_; else if (str == "FIND") sta = find_; else if (str == "TERMINATE") sta = terminate_; switch (sta) { case(0): { cout << str << endl; break; } case(1): { cout << str << sta << endl; int p(0), q(0), i(0), j(0); while (cin >> p) { cin >> q; for (i = p; i != system[i]; i = system[i]); for (j = q; j != system[j]; j = system[j]); if (i == j) continue; if (size[i] < size[j]) { system[i] = j; size[j] += size[i]; } else { system[j] = i; size[i] += size[j]; } cout << p << " - " << q << endl; } cout<<"break"<<endl; break; } case(2): { cout << str << sta << endl; break; } } } while (sta); } しかし unionを入力しあと ; でwhile(cin>>p)をブレイクしたら cin break UNION1 cin break Union1 で無限ループ 結構時間かかったが間違いがわかりません ちなみに最少は while(cin>>p>>q)と書いていましたが同じ結果です。 どうかお願いします

  • [C++]ファイル出力について

    教えてください。 コマンドプロンプトから何行か書いた文章をファイルにしたいのですが、 うまくいきません。 作ったものの結果は1行1文字で出力されて、eofでうまく 終わってくれません。 コマンドプロンプトで入力(改行)された通りにファイルに出力 できるようにしたいので、どこがおかしいのか指摘お願いします。 #include <iostream> #include <fstream> #include <stdlib.h> #include <string.h> #include <string> using namespace std; int main(int argc, char *argv[]) { char str[100]; int i; i = 0; if (argc <= 1) { cerr << "Need filename" << endl; exit (1); } ifstream ifs(argv[1], ios::in); if (ifs) { cerr << "Caution" << argv[1] << " already exists " << endl; cerr << " Specify a different filename " << endl; exit (2); } ofstream ofs(argv[1], ios::out); if (!ofs) { cerr << " Unable to write to " << argv[1] << endl; exit (3); } while(str[i] !=EOF) { cin >> str[i]; ofs << str[i] << endl; i++; } return 0; }

専門家に質問してみよう