#include <iostream>
#include <vector>
#include <string>
usingnamespace 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
}
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 ?
Create a words2 string. Loop through the words string, and copy every letter to words2, ignoring the spaces and converting uppercase to lowercase. Check the cctype library.
// start points to the beginning of the data and end points to one past the end
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
template<class T>
bool IsPalindrome(T start, T end){
if ( start == end || start == std::prev( end ) )
returntrue;
end--;
while (start <= end){
if (*start == *end){
start++;
end--;
}
elsereturnfalse;
}
returntrue;
}
ostream & operator<<(ostream &, vector<int> &);
int main()
{
string word = "GACTTCAG"; // longest palindrome in English
vector<int> number;
for (int i = 0; i < 5; i++)
number.push_back(i);
for (int j = 4; j >= 0; j--)
number.push_back(j);
if (IsPalindrome( word.begin(), word.end())){
string reverse( word.rbegin() , word.rend() );
cout << "Forward : " << word << endl
<< "Backward : " << reverse << endl
<< "It is a palindrome." << endl << endl;
}
else{
string reverse( word.rbegin() , word.rend() );
cout << "Forward : " << word << endl
<< "Backward : " << reverse << endl
<< "It is not a palindrome." << endl << endl;
}
if (IsPalindrome(number.begin(), number.end())){
vector<int> Reversenumber ( number.rbegin() , number.rend() );
cout << "Forward : " << number << endl
<< "Backward : " << Reversenumber << endl
<< "It is a palindrome." << endl;
}
else{
vector<int> Reversenumber ( number.rbegin() , number.rend() );
cout << "Forward : " << number << endl
<< "Backward : " << Reversenumber << endl
<< "It is not a palindrome." << endl;
}
system( "pause" );
return 0;
}
ostream & operator<<(ostream & stream, vector<int> & vec)
{
for(int i = 0; i < vec.size(); i++)
cout << vec[i];
return stream;
}
my question is Write a C++ program that reads a string/sequence from standard input till EOF. The string/sequence, including all white space, punctuation and newline characters should be read in character-by-character and stored into a vector. The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector.
so did i do it correctly?