c++编程回文

来源:百度知道 编辑:UC知道 时间:2024/06/30 17:00:57
回文是指顺读和倒读都一样的单词。编写一个程序,让它找出输入的单词集合中的所有回文,并指出最长的回文。谢谢各位了

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int isHuiwen(const string& str){
for(int i=0;i<str.length()/2;i++)
if(str[i]!=str[str.length()-i-1]) return 0;
return str.length();
}
int main(){
int n,max=0;
string word,maxWord;
vector<string> hwWords;
cout<<"input the number of words : ";
cin>>n;
for(int i=0;i<n;i++){
cin>>word;
if(isHuiwen(word)){
hwWords.push_back(word);
if(word.length()>max){
maxWord=word;
max=word.length();
}
}
}
cout<<"hui wen word list is:"<<endl;
for(int j=0;j<hwWords.size();j++)
cout<<hwWords[j]<