我需要创建一个程序,允许用户输入一个字符串,然后我的程序将检查该字符串是否为回文(可以正反读取的单词)。
我需要创建一个程序,允许用户输入一个字符串,然后我的程序将检查该字符串是否为回文(可以正反读取的单词)。
请注意,完全颠倒字符串(使用 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";
}
只需将字符串与其反转后的形式进行比较:
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] == '\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;
}
char*
和strlen
(因为它们更容易出错且不够灵活)。 - sehe// 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;
}
从两端开始检查字符串并在中间相遇。如果存在差异,则返回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");
}
#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;
}
将字符串反转并检查原始字符串和反转后的字符串是否相同
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);
}
Let L = Length of S
比int length = s.Length
更不容易让人翻白眼) - HostileFork says dont trust SE