Palindrome Program

Hey there again fellow code monkeys!

I'm stuck on a program. I need to prompt for the user to enter a potential palindrome ( so the usual cout cin right?) Read the user input in to a string object (the test input to your program may contain spaces but will not contain punctuation or mixed-case letters) // not sure what exactly its asking

Write a message stating that the input was or was not a valid palindrome
Your program may assume that all characters are in one case and that the input does not contain any punctuation (aside from spaces)

I'm given the following to remove spaces:

1
2
3
4
5
6
int x = s.find(' ');
while(x != -1 && x < (signed)s.size())
{
s.erase(x, 1);
x = s.find(' ');
}


aside from what was given in class (see below)
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

#include <iostream>
#include <string>
using namespace std;

string to_string(int n);
string reverse (string s);
int main ()
{
for (int number = 1; ;number++)
{
int square = number * number;

string s = to_string(square);

if (s.length() < 6)
continue;

if (s.find('0')== 0 || s.rfind('0') == s.length() - 1)
continue;

if (s == reverse (s))
{
cout << number << " " << square << endl;
break; 
}

/*int i; 

for (i=0; i < (signed)s.length() / 2; i++)
if (s[i] != s[s.length() - 1 -i])
break;

if (i == s.length() / 2)
{
cout << number << " " << square << endl;
break;
}
*/
}

return 0;
}

string reverse (string s)
{
string r; 

for (unsigned i = 0; i < s.length(); i++)
r += s[s.length()- 1 -i];

return r;
}

string to_string(int n)
{
string s;
string sign = "";

if(n <0)
{
sign = '-';
n *= -1;
}

do
{
s = (char)(n %10 + '0') + s; 
n /=10;

}while (n);

return sign + s;
}


and the cstring method:

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

#include <iostream> 
#include <cstring>
using namespace std;
// 6 digits in length, cannot begin or end with zero
// Palindrome

//solution by case

void itoa(int n, char* s);
void reverse (char*s);

int main ()
{

for (int number =1; ;number++)
{
int square =number * number;
char s[100];

itoa(square, s);

// _itoa(square, s, 10);  common function, not universal (integer to ASCII)

// _itoa_s(square, s, 100, 10) windows safe function; Microsoft only

if (strlen(s) < 6)
continue;

if (s[0] == '0' || s[strlen(s) - 1] == '0')
continue;


char r[100]; // veresion 2

strcpy(r,s);

reverse(r);

if (strcmp (s,r) == 0)
{
cout << number  << " " << square << endl;
break;
}

/*int i; //Version 1
for ( i = 0;  i< (signed)strlen(s)/2; i++)
if(s[i] != s[strlen(s) - 1-i])
break;


if (i == strlen(s) /2)
{
cout << number << " " << square << endl;
break;


}
*/
}

return 0;
}

void reverse (char*s)
{
for (int i=0; i<strlen(s) /2; i++)
{
char temp = s[i];
s[i] = s[strlen(s) - 1 -i];
s[strlen(s) - 1 -i] =temp;
}

}




void itoa(int n, char* s)
{
bool sign = n < 0;
char stack[20];
int sp = 0; //stack pointer

do
{
stack[sp++] = (char)(n % 10 + '0');
n /=10;
} while (n);

if (sign)
stack[sp++] = '-';

int index = 0; 
while (sp)
s[index++] = stack[--sp]; 

s[index] = '\0';
}


Given that code you would think that I would be able to understand it... NOPE :( any help would be appreciated
an idea might be to have a list of palindromes in an array that a reference can compare to. Otherwise this seems to be extremely complicated since everything has to be set in order to be compared to and without a formula for setting such a sequence with unknown size sounds quite complicated. ^_^ It could be easier to set a palindrome size limit or just a simple word like godsdog compared to something extreme like go hang a salami im a lasagna hog Some sort of symmetrical formula would be of interest here. :)


possibly a limit like : 123456789
that could be reversed to match 987654321 reversing strings.

then you would have to somehow take into account "whitespace" or where the empty spaces resolve and then disregard them.
Last edited on
Topic archived. No new replies allowed.