回文是指正读反读均相同的字符序列。如"abba"和”abdba”均是回文,但"good"不是回文。

Solution1

使用reverse()函数将字符串反转,与原字符串比较,若相同,则是回文,否则不是。例如:将"abba"反转得到"abba",和原字符串相同。将"good"反转得到"doog",和原字符串不同。

Code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {string a;cin >> a;string b = a;reverse(b.begin(), b.end());//反转字符串if (a == b) cout << a << "是回文串" << endl;else cout << a << "不是回文串" << endl;return 0;
}
Solution2

利用双指针,从两边往中间扫

Code1
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){string a;cin>>a;string b=a;int l=0,r=b.length()-1;while(l<r){swap(b[l],b[r]);l++;r--;}if (a == b) cout << a << "是回文串" << endl;else cout << a << "不是回文串" << endl;return 0;
}
Code2
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){string a;cin>>a;int l=0,r=a.length()-1;while(l<r){if(a[l] != a[r]) break;l++;r--;}if (l < r) cout << a << "不是回文串" << endl;else cout << a << "是回文串" << endl;return 0;
}
Solution3

利用栈后进先出的特点,将字符串的每个字符都入栈,再逐个出栈,可得到反读的字符串。可以利用STL库的stack,方便实现。

Code
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
int main() {stack<char> p;string a;cin >> a;for (int i = 0; i < a.length(); i++) p.push(a[i]); //入栈string b;while (!p.empty()) {//判断栈非空b += p.top();  //取栈顶元素p.pop();       //栈顶元素出栈}if (a == b) cout << a << "是回文串" << endl;else cout << a << "不是回文串" << endl;return 0;
}
Solution4

最近在看数据结构的书,所以自己实现了一下链栈(STL它不香吗)

Code
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
struct Node
{char data;Node* next;
};
void great_list(Node*& head)  //创建空链表无头结点
{head = NULL;
}
void set_list(Node*& head,string s) { //将一个字符串的每个字符入栈Node* p;for (int i = 0; i < s.length(); i++) {p = new Node;p->data = s[i];p->next = head;head = p;}
}
void pop(Node*& head) {//栈顶元素出栈Node* p = head;head = head->next;delete p;
}
char top(Node*& head) { //取栈顶if (head) return head->data;
}
int main() {Node* head;great_list(head);string a;cin >> a;string b;set_list(head, a);//入栈while (head){//判断栈非空b += top(head);//取栈顶元素pop(head);//栈顶元素出栈}if (a == b) cout <<a<< "是回文串" << endl;else cout <<a<< "不是回文串" << endl;return 0;
}
Sample
abbba
abbba是回文串

c++判断字符串是否为回文相关推荐

  1. 1.7-33编程基础之字符串 33:判断字符串是否为回文

    33:判断字符串是否为回文 查看提交统计提问 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个字符串,输出该字符串是否回文.回文是指顺读和倒读都一样的字符串. 输入 输入为一行字 ...

  2. C语言试题三十一之判断字符串是否为回文?若是则函数返回1,主函数中输出yes,否则返回0,主函数中输出no。回文是指顺读和倒读都是一样的字符串。

    1. 题目 请编写函数function,该函数的功能是:判断字符串是否为回文?若是则函数返回1,主函数中输出yes,否则返回0,主函数中输出no.回文是指顺读和倒读都是一样的字符串. 2 .温馨提示 ...

  3. 判断字符串是否构成回文_构成字符串回文的最小删除数

    判断字符串是否构成回文 Problem statement: 问题陈述: Given string str find the minimum number of deletions such that ...

  4. bigdecimal 判断是否为数字_C语言判断字符串是否为回文

    回文就是字符串中心对称,如"abcba"."abccba"是回文,"abcdba"不是回文. /*判断字符串是否为回文*/ #include ...

  5. 1.7 编程基础之字符串 33 判断字符串是否为回文 python

    http:// http://noi.openjudge.cn/ch0107/33/ """1.7 编程基础之字符串 33 判断字符串是否为回文http://noi.op ...

  6. 信息学奥赛一本通 1146:判断字符串是否为回文 | OpenJudge NOI 1.7 33:判断字符串是否为回文

    [题目链接] ybt 1146:判断字符串是否为回文 OpenJudge NOI 1.7 33:判断字符串是否为回文 [题目考点] 1. 字符串遍历 如何遍历字符串的一半? 如果字符串长度为len,那 ...

  7. 用递归方法判断字符串是否是回文(Recursion Palindrome Python)

    所谓回文字符串,就是一个字符串从左到右读和从右到左读是完全一样的.比如:"level" ."aaabbaaa". "madam"." ...

  8. 判断相等_C语言判断字符串是否为回文

    回文就是字符串中心对称,如"abcba"."abccba"是回文,"abcdba"不是回文. /*判断字符串是否为回文*/ #include ...

  9. 判断字符串是否为回文串

    判断字符串是否为回文串 判断回文数(双指针法) 常规解法 public boolean IsPalindrome1(String A){char[] arrayA = A.toCharArray(); ...

  10. 判断字符串是否为回文的三种常用编程语言实现

    引言:回文是一种具有镜像对称性的字符串,即它从左到右读和从右到左读是相同的.回文可以在文学.语言学.数学.计算机科学等领域中得到广泛应用.在计算机科学中,判断一个字符串是否为回文是一项基本的算法挑战. ...

最新文章

  1. LOJ:蚂蚁之旅(欧拉回路)
  2. ASP.NET数据库访问系列教程01-概述篇 创建数据访问层(中)
  3. leetcode880.DecodedStringatIndex
  4. 平板集热器的集热量和热效率计算的Python程序
  5. 中文版orgin图像数字化工具_Engauge Digitizer官方版-图形数字化处理软件下载 v10.8 官方版 - 安下载...
  6. 关于sfc /scannow后主题文件的重置
  7. 史上最全软件测试Web测试要点,吐血整理。
  8. 【CicadaPlayer】av_rescale_q 学习:转换PTS和Duration
  9. PHP队列的实现,看完秒懂
  10. elementui 描述列表Descriptions组件宽度修改
  11. html中左三角怎么写,css3三角形怎么写?
  12. 【林林js笔记】克隆数组的几种方法以及浅克隆深克隆误区
  13. LeetCode | 521. Longest Uncommon Subsequence I
  14. cocos2d-x3.x 游戏手柄
  15. 大张伟侮辱了恩克,优酷侮辱了世界杯
  16. 仿QQ浏览器mac版官网主页 html+css3特效
  17. (信贷风控九)行为评分卡模型python实现(详细代码+注释+讲解)
  18. 【Java基础语法】安排,写的太好了
  19. isotropy与anisotropy?各向同性与各向异性滤波?
  20. JAVA趣味题(答案下页慢慢答)

热门文章

  1. 越南出台利好政策促进光伏发展
  2. 同一局域网下Windows电脑和IOS设备共享文件,不需要安装软件
  3. python数据分析----matplotlib、numpy
  4. Android中显示网络图片
  5. Eigen欧拉变换演示
  6. IDEA使用git提交代码
  7. 除了花瓣 还有什么好用的素材网站?
  8. 加密狗+AES算法在QT Windows下的加密简析例程
  9. 仿网易新闻客户端UI界面小Demo
  10. android中service名词解释,Android中Service(服务)详解