Palindrome program. NOT READING SPACES OR CAPITAL LETTERS

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*This program allows the user to choose between the option of determining if the word, phase, and/or sentence 
is a palindrome or comparing two integers to see the number of equvilent integers entered at runtime.
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;

void showMenu();
void showChoices(double);

//This allows the string to center characters to the center.
//Also this is where you clairfy what the functions are.
void center(string);
bool YPalindrome(char* Palindrome);
int i;
int main()
{
	int FPint [10] = {i};
	int SPint [10] = {i};
	char FPalindrome [256];
	bool PDrome_check;
	double choices;
	const double PDrome = 1, TwoInt = 2, Close_Program = 3;
	
	//This code allows to digits after the decimal point. 
	cout << fixed << showpoint << setprecision(2);
	
	do
	{
		system("cls");
		showMenu();
		cin >> choices;

		while (!(choices >= PDrome) || !(choices <= Close_Program))
		{
			cout << endl;
			center("OOpps!! Invaild Option");
			cout << endl;
			center("Please Re-Enter your choice: ");
			cin.clear();
			fflush(stdin);
			cin >> choices;


		}
		//These if statements goes through the options and once an option is chosen then the operation is taken.
		if (choices == 1)
			{
			    cout << "Enter a word, phase, or sentence: ";
				cin >> FPalindrome;

				PDrome_check = YPalindrome(FPalindrome);
				
				if(PDrome_check == true)
				{
					cout << "Bingo! Yes, It's a Palindrome!";
				}
				else 
				{
					cout << "Nope! That's not a Palindrome.";
				}

				cin.clear();
				fflush(stdin);
			



			fflush(stdin);
			cout << endl << endl << endl;
			center("Press the Enter key to continue.");
			cin.get();
		
		}

		else if (choices == 2)
		{
			cout << "Enter the First positive integer: ";
			cin >> FPint[10];
			cout << "Enter the Second positive integer: ";
			cin >> SPint[10];
			cout << endl;
			cout << "Number 1: " << FPint[10] << endl;
			cout << "Number 2: " << SPint[10] << endl << endl;

			for (int i = 0; i <= FPint[10]; i++)
			{
			if (FPint [i] == SPint [i])
			{
				cout << FPint [i] << " <---> " << SPint [i] << " <Bingo!>" << endl;
			}
			else
			{
				cout << FPint [i] << " <---> " << SPint [i] << endl;
			}

			}
			fflush(stdin);
			cout << endl << endl << endl;
			center("Press the Enter key to continue.");
			cin.get();
		
		}


	}
	
	while (choices != Close_Program);
	fflush(stdin);
	cout << endl << endl << endl;
	center("Program was created by Tan Nguyen.\n");
	cout << endl;
	center("Press Enter key to end the Program.");
	cin.get();
	return 0;
}

//This function shows the menu presented at the beginning.
	void showMenu()
{
	cout << endl;
	center(" Menu\n");
	cout << endl;
	center("=~=~=~=~=~=~=~=~=~=~=~=");

	cout << endl << endl;
	center("1  Palindrome           ");
	cout << endl;
	center("2  Compare Two Integers ");
	cout << endl;
	center("3  End the Program.     ");
	cout << endl << endl;

	center( "Please Enter your choice:  ");
		
}

	// This portion is a function that sets a center for certain strings.
	void center(string screen)
{
	int value = (80 - (screen.length())) / 2;

	for (int i = 1; i <= value; i++)
		cout << " ";

		cout << screen;
}
	
bool YPalindrome(char* Palindrome)
{
    char* front; 
    char* rear;  
	

	front = Palindrome;// starts at the left side 
	rear = (Palindrome + strlen(Palindrome)) - 1;//starts at the right side, plus the incriment 
		while (front <= rear)
	{
		if ((*front) == (*rear) || (isspace(*front) || (isspace(*rear))))
		{
	
			do 
			{
			    front++;
				
			} while((' ' == *front) || (ispunct (*front)));
			
			do
			{
			    rear--;
				
			} while((' ' == *rear) || (ispunct (*rear)));
		}
		
		else if(!(*front == *rear))
		{
			if ((' ' == *front) || (ispunct (*front)))
			{
				front++;
			}
			if ((' ' == *rear) || (ispunct (*rear)))
			{
				rear--;
			}
			else 
			{
			 return	false;
			} 
		}
		
		else
		{
			return false;
		}
	}
    return true;
}
Last edited on
You are advancing front and rear regardless of whether or not one of them is a character that you don't do wish to have influence if the string is interpreted as a palindrome when either one is a space, and you are not accounting for punctuation where you need to.

http://www.cplusplus.com/reference/cctype/tolower/
http://www.cplusplus.com/reference/cctype/isalpha/

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
#include <cstring>
#include <cctype>

bool YPalindrome(const char* candidate)
{
    const char* left = candidate;
    const char* right = candidate + std::strlen(candidate) - 1;

    while (left <= right)
    {
        bool leftAlpha  = std::isalpha(*left);
        bool rightAlpha = std::isalpha(*right);

        if (leftAlpha && rightAlpha)        // only compare alphabetic characters.
        {
            if (std::tolower(*left) != std::tolower(*right))
                return false;

            ++left, --right;
        }
        else
        {
            if (!leftAlpha)     // skip non-alphabetic characters.
                ++left;

            if (!rightAlpha)
                --right;
        }
    }

    return true;
}
Last edited on
I edited the part with your code but it stills doesn't seem to accept capital letters and spaces well it accepts the capital letter at the beginning but when i put a space like (Ra cecar) it doesnt say that it is a palindrome
Last edited on
And also i dont quite understand the order of how your putting things because isnt int main() usually at the beginning not the end
Topic archived. No new replies allowed.