检查字符串是否为回文
Check if a string is palindrome
我需要创建一个程序,允许用户输入一个字符串,我的程序将检查他们输入的字符串是否是一个回文(可以向后读和向前读相同的单词)
注意,反转整个字符串(使用rbegin()
/rend()
范围构造函数或std::reverse
)并将其与输入进行比较将执行不必要的工作。
将字符串的前半部分与后半部分反向比较就足够了:
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string s;
std::cin >> s;
if( equal(s.begin(), s.begin() + s.size()/2, s.rbegin()) )
std::cout << "is a palindrome.n";
else
std::cout << "is NOT a palindrome.n";
}
演示:http://ideone.com/mq8qK
将字符串与自身反向比较:
string input;
cout << "Please enter a string: ";
cin >> input;
if (input == string(input.rbegin(), input.rend())) {
cout << input << " is a palindrome";
}
string
的构造函数接受起始和结束迭代器,并从这两个迭代器之间的字符创建字符串。由于rbegin()
是字符串的末尾,对它进行自增是向后的,因此我们创建的字符串将把input
的字符反向添加到它上面,从而反转字符串。
然后将其与input
进行比较,如果它们相等,则它是一个回文。
这没有考虑到大写或空格,所以你必须自己改进。
bool IsPalindrome(const char* psz)
{
int i = 0;
int j;
if ((psz == NULL) || (psz[0] == ' '))
{
return false;
}
j = strlen(psz) - 1;
while (i < j)
{
if (psz[i] != psz[j])
{
return false;
}
i++;
j--;
}
return true;
}
//STL字符串版本:
bool IsPalindrome(const string& str)
{
if (str.empty())
return false;
int i = 0; // first characters
int j = str.length() - 1; // last character
while (i < j)
{
if (str[i] != str[j])
{
return false;
}
i++;
j--;
}
return true;
}
// The below C++ function checks for a palindrome and
// returns true if it is a palindrome and returns false otherwise
bool checkPalindrome ( string s )
{
// This calculates the length of the string
int n = s.length();
// the for loop iterates until the first half of the string
// and checks first element with the last element,
// second element with second last element and so on.
// if those two characters are not same, hence we return false because
// this string is not a palindrome
for ( int i = 0; i <= n/2; i++ )
{
if ( s[i] != s[n-1-i] )
return false;
}
// if the above for loop executes completely ,
// this implies that the string is palindrome,
// hence we return true and exit
return true;
}
#include <iostream>
#include <string>
bool isPalindrome(const std::string& str){
if(str.empty()) return true;
std::string::const_iterator itFirst = str.begin();
std::string::const_iterator itLast = str.end() - 1;
while(itFirst < itLast) {
if (*itFirst != *itLast)
return false;
++itFirst;
--itLast;
}
return true;
}
int main(){
while(1){
std::string input;
std::cout << "Eneter a string ...n";
std::cin >> input;
if(isPalindrome(input)){
std::cout << input << " is palindrome.n";
} else {
std::cout << input << " is not palindrome.n";
}
}
return 0;
}
检查字符串从两端开始到中间相交。如果不一致,返回false。
#include <iostream>
bool palidromeCheck(std::string str) {
for (int i = 0, j = str.length()-1; i <= j; i++, j--)
if (str[i] != str[j])
return false;
return true;
}
int main(){
std::cout << palidromeCheck("mike");
std::cout << palidromeCheck("racecar");
}
反转字符串并检查原字符串与反转字符串是否相同
我不是c++爱好者,但你应该能够从中得到要点。
public static string Reverse(string s) {
if (s == null || s.Length < 2) {
return s;
}
int length = s.Length;
int loop = (length >> 1) + 1;
int j;
char[] chars = new char[length];
for (int i = 0; i < loop; i++) {
j = length - i - 1;
chars[i] = s[j];
chars[j] = s[i];
}
return new string(chars);
}
相关文章:
- 如何检查C ++ STL列表是否为回文?
- C++ - 检查结构数据类型中的单词是否为回文
- 确定用户输入是否为带C++的回文
- 是否可以检查存储在堆栈上的单词是否是回文,而C++中没有任何附加数据结构
- 任何人都可以检查这个回文 c++ 代码是否正确
- 寻找一种更有效的方法来使用 STL 函数检查字符串是否为回文
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 如何在不使用数学的情况下知道一个数字是否是回文
- 内联程序集 - 检查单词是否为回文
- 检查子数组是否为带指针的回文数组
- 验证字符串是否为回文的函数
- 验证代码以检查数字是否为回文
- 检查数字是否是C++中的回文
- 需要修改堆栈/链表程序以确定单词是否为回文
- 检查数字是否为回文
- 看看我们是否可以获得回文
- 使用不同的函数(C++)测试一个数字是否是回文
- 检查字符串是否为回文
- 目的:检查用户输入的字符串,以确定它是否为回文
- C++-使用堆栈来确定C样式字符串是否为回文