检查 C++/Java 中的回文

发布于 2024-11-13 02:37:59 字数 270 浏览 5 评论 0原文

可能的重复:
检查字符串是否为回文

专家您好。有人问我,是否可以仅用 C++/Java 中的一行代码来查找一个字符串是否是另一个字符串的回文。

如果是,那么如何?

有谁能解答一下吗。感谢您的观点。

Possible Duplicate:
Check string for palindrome

Hello experts. I am asked, if it is possible to find whether a string is palidrome of another string in just one line of code in C++/Java.

If yes, then how?

Can any one answer. Thnx for ur view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍白女子 2024-11-20 02:37:59

在 Java 中,String 没有 reverse 方法。不过 StringBuilder 有,所以您仍然可以在一行中完成:

boolean palindrome = str.contentEquals(new StringBuilder(str).reverse());

Ideone.com demo

In Java, String does not have a reverse method. StringBuilder has though, so you can still do it in one line:

boolean palindrome = str.contentEquals(new StringBuilder(str).reverse());

Ideone.com demo

蘸点软妹酱 2024-11-20 02:37:59

使用reverse 反转字符串,然后与原始字符串进行比较。

String str="ABBA"
if(str.COmpareTo(str.reverse()) ==0)
//String is palindrome.

Reverse the string using reverse and then compare with original string.

String str="ABBA"
if(str.COmpareTo(str.reverse()) ==0)
//String is palindrome.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文