C++言語解説:1-5-2.課題回答
2002-06-02

 [課題内容]
   ┌───────────────────────────────────┐
    [演習課題]
    (1)文字列"Hello"をデータに持つchar型配列s1から、char型配列s2にデータを
      コピーし、画面にs1, s2の内容を表示すること。
      尚、文字配列は以下の形で初期化定義可能である(添字省略可能)。
       char s1[] = "Hello";
    
    (2)文字列データ"Good"を持つ文字型配列s1に、文字列データ"Morning"を連結
      して表示すること。その際、GoodとMorningの間には1byteスペースを入れ
      ること。
    
    (3)文字型配列s1は、null文字を含めて80文字までのデータを持てる。キーボ
      ードから入力した80文字未満の任意文字列データを画面表示後、全ての文
      字データを * へ置換して再度表示すること。
    
    (4)パスワードは"I am Great"である。キーボードから入力された文字列がパ
      スワードと一致する場合は"Welcome!!"を画面に表示し、パスワードが異な
      る場合は"Get Out!!"と表示すること。
    
    (5)文字型配列s1は、null文字を含めて80文字までのデータを持てる。キーボ
      ードから入力した80文字未満の任意文字列データを画面表示後、最初の文
      字'A'のある配列添字番号を表示すること。'A'が存在しない場合には
      "Not found"を画面へ表示すること。
   └───────────────────────────────────┘

 [課題回答]

  (1)の回答例
   ┌───────────────────────────────────┐
    #include <iostream>
    #include <cstring> //<string.h>
    using namespace std;
    
    int main()
    {
      char s1[] = "Hello";
      char s2[6]; //Hello(5文字) + null文字
      
      strcpy(s2, s1);
      
      cout << "s1 :" << s1 << endl;
      cout << "s2 :" << s2 << endl;
      
      return 0;
    }
   └───────────────────────────────────┘
   ┌───────────────────────────────────┐
    [実行結果]
     s1 :Hello
     s2 :Hello
   └───────────────────────────────────┘

  (2)の回答例
   ┌───────────────────────────────────┐
    #include <iostream>
    #include <cstring> //<string.h>
    using namespace std;
    
    int main()
    {
      char s1[15] = "Good";
      
      strcat(s1, " ");
      strcat(s1, "Morning");
      
      cout << "s1 :" << s1 << endl;
      
      return 0;
    }
   └───────────────────────────────────┘
   ┌───────────────────────────────────┐
    [実行結果]
     s1 :Good Morning
   └───────────────────────────────────┘

  (3)の回答例
   ┌───────────────────────────────────┐
    #include <iostream>
    #include <cstring> //<string.h>
    #include <cstdio> //<stdio.h>
    using namespace std;
    
    int main()
    {
      char s1[80];
      size_t st_i;
      
      cout << "Input Data :";
      gets(s1);
      cout << "s1 :" << s1 << endl;
      
      for (st_i=0; st_i<strlen(s1); st_i++) {
        s1[st_i] = '*';
      }
      
      cout << "s1 :" << s1 << endl;
      
      return 0;
    }
   └───────────────────────────────────┘
   ┌───────────────────────────────────┐
    [実行結果]
     Input Data :abcd efg
     s1 :abcd efg
     s1 :********
   └───────────────────────────────────┘

  (4)の回答例
   ┌───────────────────────────────────┐
    #include <iostream>
    #include <cstring> //<string.h>
    #include <cstdio> //<stdio.h>
    using namespace std;
    
    int main()
    {
      char s1[80];
      
      cout << "Input Password :";
      gets(s1);
      
      if (!strcmp("I am Great", s1)) {
        cout << "Welcome!!" << endl;
      }
      else {
        cout << "Get Out!!" << endl;
      }
      
      return 0;
    }
   └───────────────────────────────────┘
   ┌───────────────────────────────────┐
    [実行結果]
     C:\Tmp>test
     Input Password :I am outsider
     Get Out!!

     C:\Tmp>test
     Input Password :I am Great
     Welcome!!
   └───────────────────────────────────┘

  (5)の回答例
   ┌───────────────────────────────────┐
    #include <iostream>
    #include <cstring> //<string.h>
    #include <cstdio> //<stdio.h>
    using namespace std;
    
    int main()
    {
      char s1[80];
      char* cp_locate;
      
      cout << "Input :";
      gets(s1);
      cout << "s1 :" << s1 << endl;
      
      cp_locate = strchr(s1, 'A');
      
      if (cp_locate != NULL) {
        cout << "添字番号 =" << cp_locate - s1 << endl;
      }
      else {
        cout << "Not found" << endl;
      }
      
      return 0;
    }
   └───────────────────────────────────┘
   ┌───────────────────────────────────┐
    [実行結果]
     Input :abcdABCD
     s1 :abcdABCD
     添字番号 =4
   └───────────────────────────────────┘


[Revision Table]
 |Revision |Date    |Comments
 |----------|-----------|-----------------------------------------------------
 |1.00   |2002-06-02 |回答隠蔽版
 |1.01   |2002-06-02 |回答開放版
 |1.02   |2002-06-08 |回答開放(修正)版
[end]
Copyright(C) 2002 Altmo