C++ 使用栈判断回文字符串
2016-02-03 14:37
549 查看
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
stack<char> st1;
stack<char> st2;
stack<char> st3;
string str;
cout << "enter the string: " << endl;
getline(cin,str);
int len = str.length();
int i =0;
while(i<len/2){
st1.push(str[i]);
i++;
}
if(len%2==1)
i++;
while(i<len){
st2.push(str[i]);
i++;
}
while(!st2.empty()){
char ch = st2.top();
st3.push(ch);
st2.pop();
}
while(!st1.empty()){
if(st1.top()==st3.top()){
st1.pop();
st3.pop();
}
else{
cout <<"the string "<<str<<" is not palindrome\n";
return 0;
}
}
cout <<"the string "<<str<<" is palindrome\n";
return 0;
}
#include <string>
#include <stack>
using namespace std;
int main()
{
stack<char> st1;
stack<char> st2;
stack<char> st3;
string str;
cout << "enter the string: " << endl;
getline(cin,str);
int len = str.length();
int i =0;
while(i<len/2){
st1.push(str[i]);
i++;
}
if(len%2==1)
i++;
while(i<len){
st2.push(str[i]);
i++;
}
while(!st2.empty()){
char ch = st2.top();
st3.push(ch);
st2.pop();
}
while(!st1.empty()){
if(st1.top()==st3.top()){
st1.pop();
st3.pop();
}
else{
cout <<"the string "<<str<<" is not palindrome\n";
return 0;
}
}
cout <<"the string "<<str<<" is palindrome\n";
return 0;
}
相关文章推荐
- C++学习之多态篇(运行时类型识别--RTTI(typeid和dynamic_cast))
- 两道出现频率超高的C++笔试题
- NSScanner: nil string argument libc++abi.dylib: terminate_handler unexpectedly threw an exception问题
- C语言之基本算法35—数组上三角之积 主对角之积 副对角之积
- Effective C++ 05:了解C++默默编写并调用哪些函数
- [C++]Hanoi
- C++中智能指针的设计和使用
- 【转】C++11 标准新特性:Defaulted 和 Deleted 函数
- C++如何拒绝编译器自动生成的函数
- c++ for_each 用法
- 第二次作业----自学c++的选择与计划
- C++学习之多态篇(纯虚函数和虚函数的区别)
- C语言字符串操作总结大全(超详细)
- C++学习之多态篇(纯虚函数和接口类)
- C++实现大数据乘法
- C++中vector的用法实例解析
- C++ string 字符串详解
- C++ 总结
- Tencent-电话面试C语言问题汇总
- 《Effective C++》学习笔记(一)