國立屏東大學 資訊工程系 物件導向程式設計

hw1-2 參考解答


using namespace std;
#include <iostream>
 
int main()
{
  string str;
  cout << "Please input a name:" ;
  getline(cin,str);
  if(str.find('-')==-1)
  {
    int f=str.find(' ')+1;
    int l=str.length();
    cout << "The initials are " <<str[0] << ". " << str.substr(f,l-f);
    cout << " and " << str.substr(f,l-f) << ", " << str[0] << ".\n";
  }
  else
  {
    int f=str.find(' ')+1,l=str.length(),d=str.find('-')+1;
    cout << "The initials are " <<str[0] << ".-" << str[d]
          << ". "  << str.substr(f,l-f);
    cout << " and " << str.substr(f,l-f) << ", " << str[d]
          << ".-"  << str[0] << ".\n";
  }
  return 0;
}