2013-03-25 4 views
0

그래서 T've는 palindrome 프로그램을 하나의 단어 palindrome을 확인하는 백만 가지 예제를 발견했습니다.Word palindrome program by word

하지만 단어 하나 하나를 문장으로 예를 들어 설명하는 데 도움이 필요합니다. 삼키기를 할 수 있지만 감옥을 삼킬 수는 없습니까? "라는 단어가 단어 회문으로 사용됩니다. 책이 준 코드는이

// 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;  

}

+0

아직 시작점이 아닌가요? –

+0

음 ... 뭐가 문제입니까? – Asha

+0

@Asha 단어 palindrome으로 단어를하는 법을 알아야 할 필요가 있다고 생각할 수있는 유일한 방법은 문자열을 사용하는 것이지만 문자열 조각을 조각으로 튀어 나오게하는 방법을 모른다는 것입니다. – user2206227

답변

1

이 실제로 기본 코드에서. 당신은 당신의 큐에 단어 (문자열)를 추가하고 대신 문자의. 나는 빨리 스택해야 할 매우 쉽습니다 코드 수정 :

#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

+0

@ user2206227 기꺼이 도와 드리겠습니다! –

관련 문제