プログラムは一連の文字列 (名前と 8 文字の単語) をユーザーに要求し、名前、単語の最初と最後の 3 文字を出力し、単語を逆順に出力します。文字列を逆向きに表示するには、for ループのヘルプが必要です。
#include <iostream>
int main () {
string FirstName;
string LastName;
string MiddleName;
string Names;
string string1;
int len;
int x;
cout << "Hello. What is your first name?" << endl;
cin >> FirstName;
cout << FirstName << ", what is your last name?" << endl;
cin >> LastName;
cout << "And your middle name?" << endl;
cin >> MiddleName;
Names = LastName + ", " + FirstName + ", " + MiddleName;
cout << Names << endl;
cout << "Please enter a word with 8 or more characters (no spaces): " << endl;
cin >> string1;
len = string1.length();
if (len < 8){
cout << "Error. Please enter a word with 8 or more characters and no spaces: " << endl;
cin >> string1;
}
else if (len >= 8){
cout << "The word you entered has " << string1.length() << " characters."<<endl;
cout << "The first three characters are " << string1.substr(0,3) << endl;
cout << "The last three characters are " <<string1.substr(string1.length()-3,3) << endl;
x = string1.length()-1;
for (x = string1.length()-1; x >=0; x--){
cout << "Your word backwards: " << string1[x];
}
}
return 0;
}