%()Just as the string class version of the name-box program paralleled and was structurally identical to the C-string version, the string version of the palindrome-number program parallels the C-string version. Nevertheless, the string class provides some operators that ease the implementation. Significantly, it provides a more reliable conversion from numbers to strings. Implementing the palindrome validation programs, the number-to-string conversion functions, and the reverse function, demonstrate numerous string functions and operators.
#define _CRT_SECURE_NO_WARNINGS // Microsoft only
#include <iostream>
#include <string>
using namespace std;
string to_stringR2L(int n);
string to_stringL2R(int n);
int main()
{
for (int number = 1; ; number++) // (a/I.i and I.ii)
{
int square = number * number; // (b/II)
// ---------------------------------- III (c)
string s = to_string(square); // (c)
//string s = to_stringR2L(square);
//string s = to_stringL2R(square);
// ---------------------------------- IV - begin case analysis
if (s.length() < 6) // (d/IV.i)
continue;
if (s[0] == '0' || s[s.length() - 1] == '0') // (e/IV.ii)
//if (s.at(0) == '0' || s.at(s.length() - 1) == '0')
//if (s.find_first_of('0') == 0 || s.find_last_of('0') == s.length() - 1)
//if (s.find('0') == 0 || s.rfind('0') == s.length() - 1)
continue;
// ---------------------------------- IV.iii - palindrome solution
int i; // (f)
for (i = 0; i < s.length() / 2; i++) // (g)
if (s[i] != s[s.length() - 1 - i]) // (h)
break;
if (i == s.length() / 2) // (i)
{
cout << number << " " << square << endl;
break;
}
}
return 0;
}
to_string(square) function converts a number to an instance of the string class.find and find_first behave the same way when the argument is a single character (see "Searching" for more detail).i < s.length() / 2 divides the string in half, skipping the middle character if the number of characters is odd.s[i] and s[s.length() - 1 - i] are the left- and right-hand characters, respectively. break operates on the inner for-loop.break applies to the outer for-loop, ending the program.The ANSI C++ 2011 standard added the to_string function to C++ to convert numbers to strings. Before then, the palnumber example included its own version of to_string. (I'm personally amused that the ANSI committee chose the same name and syntax for their conversion functions as I did for this longstanding example.) Although writing a to_string function is no longer necessary, it demonstrates the string class's concatenation operators.
| Right To Left | Left To Right |
|---|---|
string my_to_stringR2L(int n)
{
string s;
string sign;
if (n < 0)
{
sign = "-";
n = -n;
}
do
{
s = (char)(n % 10 + '0') + s;
n /= 10;
} while (n);
return sign + s;
} |
string to_stringL2R(int n)
{
if (n == 0)
return "0";
string s;
if (n < 0)
{
s += '-';
n = -n;
}
for (int digit = (int)log10(n); digit >= 0; digit--)
s += char((n / (int)pow(10, digit)) % 10 + '0');
return s;
} |
|
|
+ and +=, respectively, support the conversion process in two ways: First, the operators allow individual characters, and second, they allow concatenation on the left side in addition to the right. Both versions convert their parameter, n, to a string object by isolating each digit, converting it to an ASCII character by adding an ASCI '0' to it, and concatenating it to a string object.
The reverse string solution is logically and structurally similar to the C-string version, and relies on the same definition of a palindrome as a string that reads the same forwards and backward (without considering spaces or other punctuation characters). The next example solves the palindrome-number problem based on the definition of a palindrome. It reverses the candidate string and compares it to the original. If and only if the two strings are the same (i.e., equal), the candidate is a palindrome.
#define _CRT_SECURE_NO_WARNINGS // Microsoft only
#include <iostream>
#include <string>
using namespace std;
string reverse(string s);
int main()
{
for (int number = 1; ; number++) // (I.i and I.ii)
{
int square = number * number; // (II)
string s = to_string(square); // (III)
//---------------------------------------------- IV - begin case analysis
if (s.length() < 6) // (IV.i)
continue;
if (s.find('0') == 0 || s.rfind('0') == s.length() - 1) // (IV.ii)
continue;
//---------------------------------------------- IV.iii - palindrome detection
if (s == reverse(s))
{
cout << number << " " << square << endl;
break;
}
}
return 0;
}
| Traditional For-Loop | For-Range Loop |
|---|---|
string reverse(string s)
{
string r;
for (int i = 0; i < s.length(); i++)
r += s[s.length() - 1 - i];
return r;
}
|
string reverse(string s)
{
string r;
for (char c : s)
r = c + r;
return r;
}
|
+=+, and assigns results back to r.| View | Download | Comments |
|---|---|---|
| palnumber1.cpp | palnumber1.cpp | string solution of the palindrome-number puzzle using the finger method |
| to_stringR2L.cpp | to_stringR2L.cpp | Number-to-string right-to-left processing order. |
| to_stringL2R.cpp | to_stringL2R.cpp | Number-to-string left-to-right processing order. |
| validate.cpp | validate.cpp | A driver validating the to_stringR2L and to_stringL2Rfunctions. |
| palnumber2.cpp | palnumber2.cpp | string solution of the palindrome-number puzzle using the reverse method |
| reverse.cpp | reverse.cpp | Function to reverse a string object |