Форум » C/C++ для начинающих (C/C++ for beginners) » Basic Palindrome vector » Ответить

Basic Palindrome vector

Felicia123: [pre2]#include <iostream> #include <vector> #include <string> using namespace std; int main(){ string words; cout <<"Enter words : " ; getline( cin,words ); string wordsReverse( words.rbegin() , words.rend() ); cout << "\nBackwards , it's : " << wordsReverse << endl << endl; if( words == wordsReverse ) cout << words << " is a palindrome !" << endl; else cout << words << " is not a palindrome ! " << endl; system( "pause" );//Pause window return 0;//Exit Program }[/pre2] i know it's not hard . but the problem is , here is the question A palindrome is a sequence of characters, numbers or words that have the property of reading the same in either direction (backwards or forwards). In a palindrome white space and punctuation have no significance (they are typically ignored). Examples of palindromes are shown below. 121 - A numeric sequence GACTTCAG - A DNA Sequence Sator Arepo tenet opera rotas - A latin phrase for: The farmer by his labour keeps the wheels to the plough. As you can see in the last example, white space and capitalisation are ignored. You are to write a program that takes in a sequence and works out if it is/isn’t a palindrome. We are going to do this in three different ways. how to ignore capitalisation and while space ? Besides that . how to change the iterator STL to vector STL ?

Ответов - 39, стр: 1 2 All

Felicia123: mind to explain what is the v.back() are? if the last character for vector v are '\n' will pop_back the vector container? and did i push correctly for stack and queue?

Сыроежка: Method back returns reference to the last element of the container. So you check whether the new line character was placed in the container because when the user was entering a sequence he pressed ENTER key. You have to remove this new line character otherwise the result of determining whether a sequence is a polindrome will be incorrect.

Felicia123: okay. same meaning as mine isn't? haha so for my push for the stack and queue . is it correct?


Сыроежка: If you have deleted that trailing new line character before pushing the sequence into the stack or the queue then all is o'k.

Felicia123: okay.. now .. move on the next way? so how should i compare for the queue and stack ? with q.front and s.top? i not really get the algorithm..

Сыроежка: It is your algorithm. Why are you asking me about it?

Felicia123: because i not really know it [pre2] while(!q.empty() && !s.empty()){ if(q.front() != s.top()){ cout << "The word "; for( i = 0 ; i < v.size() ; i++ ){ cout << v ; } cout<< " is not a palindrome" << endl; break; } else if( q.front() == s.top() ){ i++; q.pop(); s.pop(); if( i = v.size()){ cout << "The word "; for( i = 0 ; i < v.size() ; i++ ){ cout << v ; } cout<< " is not a palindrome" << endl; break; } } }[/pre2] cannot work.. what is my error ??

Сыроежка: You should not ask such questions. Why do not you testt the code yourself? 1. Negation of condition ( q.front() != s.top() ) is condition ( q.front() == s.top() ) so there is no need to write else if( q.front() == s.top() ) . It is enpugh to write simply else 2. These two conditions ( !q.empty() && !s.empty() ) and ( i == v.size() ) are reduntant. You should use only one of them. 3. Instead of using the comparision operator in condition ( i = v.size() ) you wrote the assignment operator. 4. In the both parts of the if-else-if operator you wrote that the word is not a polindrome, Then a question arises when is the word a polindrome?! I would write the loop simply [pre2] std::vector::size_type i = 0; while ( i < v.size() && q.front() == s.top() ) i++; std::cout << "The word "; for ( char c : v ) std::cout << c; std::cout << " is" << ( i == v.size() ? "" : " not" ) << " a polindrome." << std::endl;[/pre2]

Felicia123: sorry .. what is for( char : v ) because it's not work for me..

Сыроежка: It is so-called the range based for statement introduced in C++ 2011. If your compiler does not support this feature of the new C++ standard then you can change [pre2]for ( char c : v ) std::cout << c;[/pre2] to [pre2] for ( std::vector<char>::const_iterator it = v.begin(); it != v.end(); ++it ) { std::cout << *it; }[/pre2] or [pre2] for ( std::vector<char>::size_type i = 0; i != v.size(); ++i ) { std::cout << v[ i ]; }[/pre2]

Felicia123: it's can't work. some people told me i have to use v to feed my s.top and q.front? how should i feed it?

Сыроежка: It was a typo. I forgot to specify pop commands for the stack and queue. It will look the following way [pre2] std::vector::size_type i = 0; while ( i < v.size() && q.front() == s.top() ) { s.pop(); q.pop() i++; }[/pre2]

Felicia123: getting a pop message said that deque iterator not dereferencable. i think something wrong in my [pre2]stack<char> s; queue<char> q;[/pre2] isn't? ><

Сыроежка: I do not see any dereferencing in my code snip.

Felicia123: #include<iostream> #include <vector> #include<string> #include<stack> #include<queue> using namespace std; int main(){ int i = 0; char InputChar; //Vector holds an object type char vector<char> v; istream::int_type c; cout << "Enter a sequence of characters: "; while ( ( c = cin.get() ) != EOF ) { v.push_back( c ); } while ( !v.empty() && v.back() == '\n' ) v.pop_back(); stack<char> s; queue<char> q; for( i = 0 ; i < v.size() ; i++ ){ s.push(v); q.push(v); } while ( i < v.size() && q.front() == s.top() ){ s.pop(); q.pop(); i++; } cout << "The word "; for( i = 0 ; i < v.size() ; i++){ cout << v; } cout << " is" << ( i == v.size() ? "" : " not" ) << " a polindrome." << endl; system( "pause" ); return 0; } i modified my program , and the error doesn't come out anymore. but the result i get is only palindrome. even i insert the sequence that are not palindrome

Сыроежка: I do not see where the stack and the queue are being filled with values. They both are empty. Here is a simplified example that demonstrates how the task can be done [pre2] std::string str( "ABCDEFGHGFEDCBA" ); std::vector<char> v( str.begin(), str.end() ); std::stack<char> s; std::queue<char> q; for ( std::vector<char>::size_type i = 0; i < v.size(); i++ ) { s.push( v ); q.push( v ); } std::vector<char>::size_type i = 0; while ( i < v.size() && s.top() == q.front() ) { s.pop(); q.pop(); i++; } std::cout << "Word \""; for ( std::vector<char>::const_iterator it = v.begin(); it != v.end(); ++it ) { std::cout << *it; } std::cout << "\" is " << ( ( i == v.size() ) ? "" : "not " ) << "a polindrome\n"; [/pre2]

Felicia123: i already push it . mind to see the above program? i already filled it once I saw that i didn't fill. but i have the result that stated only palindrome even not palindrome

Сыроежка: [pre2] #include <iostream> #include <vector> #include <queue> #include <stack> #include <cstdlib> int main() { std::vector<char> v; std::istream::int_type c; std::cout << "Enter a sequence of characters: "; while ( ( c = std::cin.get() ) != EOF ) { v.push_back( c ); } if ( !v.empty() && v.back() == '\n' ) v.pop_back(); std::stack<char> s; std::queue<char> q; for ( std::vector<char>::size_type i = 0; i < v.size(); i++ ) { s.push( v[ i ] ); q.push( v[ i ] ); } std::vector<char>::size_type i = 0; while ( i < v.size() && s.top() == q.front() ) { s.pop(); q.pop(); i++; } std::cout << "Word \""; for ( std::vector<char>::size_type j = 0; j != v.size(); ++j ) std::cout << v[ j ]; std::cout << "\" is " << ( ( i == v.size() ) ? "" : "not " ) << "a polindrome\n"; system( "Pause" ); return ( 0 ); }[/pre2]

Felicia123: thanks for your help but i change the while loop to for loop before the pop() . and it's work. i just try to change it and it's work ! haha. thanks ><!



полная версия страницы