Basic Palindrome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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
}


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 ?
Last edited on
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.

http://www.cplusplus.com/cctype

Base wordsReverse on words2.
@felicia: you can use various ways, i guess. the first way is to use char [], and the second is to use string but they both works similar (or same)
1
2
3
if (isspace(c)) c='\n';
    putchar (c);
    i++;


just an example from the tutorial

can i remove it?
just like
 
s.erase(remove_if(s.begin(), s.end(), isspace), s.end());

isn't correct?
s.erase(remove_if(s.begin(), s.end(), isspace), s.end());
[EDIT]I'd say it's correct.It's the opposite: Make a function that negates isspace()

Besides that . how to change the iterator STL to vector STL ?
Simple as that:
1
2
3
4
std::vector<char> v;
std::string s;
...
v.assign(s.begin(), s.end());



how to ignore capitalisation

http://www.cplusplus.com/reference/clibrary/cctype/toupper/
http://www.cplusplus.com/reference/clibrary/cctype/tolower/
Last edited on
this is my current coding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// start points to the beginning of the data and end points to one past the end
#include <iostream>
#include <string>
#include <vector>

using namespace std;

template<class T>
bool IsPalindrome(T start, T end){
	if ( start == end || start == std::prev( end ) )
		return true;
	end--;
	while (start <= end){
		if (*start == *end){
			start++;
			end--;
		}
		else
			return false;
	}
	return true;
}

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?
I do not see where a sequence of characters is entered in a vector until EOF will be encountered.
err. i thought the looping 1 is already ?

i just remember that i use EOF in my read file place..
while(!outFile.eof)

but how to use at here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// start points to the beginning of the data and end points to one past the end
#include <iostream>
#include <string>
#include <vector>

using namespace std;

template<class T>
bool IsPalindrome(T start, T end){
	if ( start == end || start == std::prev( end ) )
		return true;
	end--;
	while (start <= end){
		if (*start == *end){
			start++;
			end--;
		}
		else
			return false;
	}
	return true;
}

ostream & operator<<(ostream &, vector<int> &);

int main()
{
	int i = 0;
	string word = "GACTTCAG";
	vector<int> number;
	do{
		cout << "Enter number : ";
		cin  >> i;
		if( cin.good() )
			number.push_back(i);
		else
			break;
	}while( !cin.eof() );

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



here is my current code.
i done it using
vector<int>

but instead i want to use vector<char>?
if i convert into char . why got error d?
do you know that reverse exists in the std:

http://www.cplusplus.com/reference/algorithm/reverse/

I'd say that line 74 should be: stream << vec[i]; (This makes it possible to output the numbers to a file stream)

The vector should hold objects of type char
So why do you use int?
Let the user enter a string. Implement a loop that checks with isdigit()

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

if not complain...

the vector thing is somewhat weird. I don't see any use of it.

eof for console input does not make sense (there's none). I'd make sense for a file

I showed you how to convert a string to a vector.

See http://cplusplus.com/forum/beginner/84329/#msg452121

you can 'transform' any container to another container as long as they have the same type.
Last edited on
Topic archived. No new replies allowed.