Word by palindrome program
问题描述:
因此,T've发现了一百万个回文项目的例子,用于检查单个词回文。Word by palindrome program
但是T需要帮助做一个单词一个单词,例如句子你可以把一只燕子吞噬你,但你不能吞下一只笼子吗?“这将是一个单词回文,我只需要一个跳跃开始的例子码书送给这是
// FILE: pal.cxx
// Program to test whether an input line is a palindrome. Spaces,
// punctuation, and the difference between upper- and lowercase are ignored.
#include <cassert> // Provides assert
#include <cctype> // Provides isalpha, toupper
#include <cstdlib> // Provides EXIT_SUCCESS
#include <iostream> // Provides cout, cin, peek
#include <queue> // Provides the queue template class
#include <stack> // Provides the stack template class
using namespace std;
int main()
{
queue<char> q;
stack<char> s;
char letter;
queue<char>::size_type mismatches = 0; // Mismatches between queue and stack
cout << "Enter a line and I will see if it's a palindrome:" << endl;
while (cin.peek() != '\n')
{
cin >> letter;
if (isalpha(letter))
{
q.push(toupper(letter));
s.push(toupper(letter));
}
}
while ((!q.empty()) && (!s.empty()))
{
if (q.front() != s.top())
++mismatches;
q.pop();
s.pop();
}
if (mismatches == 0)
cout << "That is a palindrome." << endl;
else
cout << "That is not a palindrome." << endl;
return EXIT_SUCCESS;
}
答
这是很容易从你的基础代码,这样做其实,你只需要添加的话(串)到您的队列。我赶紧堆栈上而不是字符修改代码:
#include <algorithm>
queue<std::string> q;
stack<std::string> s;
std::string word;
queue<std::string>::size_type mismatches = 0; // Mismatches between queue and stack
cout << "Enter a line and I will see if it's a palindrome:" << endl;
while (cin.peek() != '\n')
{
cin >> word;
std::transform(word.begin(), word.end(), word.begin(), ::toupper);
q.push(word);
s.push(word);
}
通过使用cin读取字符串,您将自动使用空格作为分隔符。该行:
std::transform(word.begin(), word.end(),word.begin(), ::toupper);
将字符串中的所有字符都转换为大写。
+0
谢谢,我知道这很简单,我只是错过了一个简单的步骤。谢谢! – user2206227 2013-03-25 05:08:05
+0
@ user2206227很高兴我能帮忙! – 2013-03-25 05:16:45
这不就是一个跳跃的开始吗? – 2013-03-25 04:55:23
嗯..问题在哪? – Asha 2013-03-25 04:56:03
@Asha我需要知道如何通过单词回文做一个单词,我能想到的唯一的事情是使用字符串,但我不知道如何弹出一个字符串片 – user2206227 2013-03-25 05:02:19