Numerical palindrome how to add leading values

Hi there,

This program is supposed to determine if a number input is a palindrome or not.
The problem I'm having is that I am supposed to add leading zeros in front of an input that has less than 5 digits as explained below:

Numbers with fewer than 5 digits are automtically "padded" with leading zeros. So if you enter 0, that gets treated as 00000, and it's a palindrome. But 1 gets treated as 00001, and it's not. 100 is, because that's 00100.

I've included pseudo code for this part but for some reason my brain is blanking on how to turn this into an actual statement. If you can provide me with an example or even just a hint, I'd really appreciate it. Thanks

1
2
3
4
//pseudo code for adding leading zeros
//determine how many characters have been input into array (length)
//create new array that adds (5-length) zeros in front of the input (this is the part I don't understand how to do)
//send the new array into the palindrome checker portion of the program. 



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

#include <cstring>
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
  //Declare variables
  char num[4];
  bool palindrome=true;
  do
  {
  cout << "Enter a five or less digit number or enter 'Q' to quit." << endl;
  cin >> num;
  if (num[0] == 'Q' || num[0] == 'q')
    break;
  //if user does not quit
  int length = strlen(num);
  int(length/2);
  if (length > 0)
    {
      for(int i = 0; i < length ;i++)
      {
        if(num[i]!= num[length-1-i])
        palindrome=false;
      }
    }
  if(palindrome==true)
  {
    cout << "The number " << num << " is a palindrome" << endl;
  }
  else
  {
    cout << "The number " << num << " is not a palindrome" << endl;
  }
  }while (/*need condition?*/);
}


Also, note that the current code is not complete. I will be finishing it at a later time. Thanks again!
For one your array holds only four characters to make it hold 5 declare it as suchchar num[5];.

To address your problem though simply have a separate string of 5 zeroes that you replace with the input char t[5] = {'0', '0', '0', '0', '0'};. Then simply replace the characters starting from 5 - the length of the input. I believe that should work.
std::string https://cal-linux.com/tutorials/strings.html makes life easier.

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
#include <iostream>
#include <string>

int main()
{
    std::cout << "enter a non-negative integer with five or fewer digits: " ;

    int number ;
    if( std::cin >> number ) // if the user entered an integer
    {
        if( number < 0 ) std::cout << "you entered a negative number\n" ;
        else if( number > 99999 ) std::cout << "that number has more than five digits\n" ;

        else // non-negative integer with five or fewer digits
        {
            // http://en.cppreference.com/w/cpp/string/basic_string/to_string
            std::string str_number = std::to_string(number) ; // convert the number to a string

            // add leading zeroes till there are five digits
            while( str_number.size() < 5 ) str_number = '0' + str_number ;

            // TODO: check if the str_number is a palindrome
            // TODO: print result
        }
    }

    else std::cout << "you did not enter an integer\n" ;
}
Thanks JLBorges! Is there a way to do this with something other than using to_string()? My compiler doesn't like it (MinGW).

Also, TooExplosive, I tried doing what you said, but I can't figure out what to put in the loop/conditions. Could you elaborate? Thanks
1
2
#include <sstream>
#include <iomanip> 

and replace lines 16 - 20
1
2
3
4
5
            // http://en.cppreference.com/w/cpp/string/basic_string/to_string
            std::string str_number = std::to_string(number) ; // convert the number to a string

            // add leading zeroes till there are five digits
            while( str_number.size() < 5 ) str_number = '0' + str_number ;

with this
1
2
3
4
5
6
7
8
            // convert the number to a string with leading zeroes added till there are five digits

            std::ostringstream stm ; // a stream that writes into a string

            // right justified (default), in a field of width 5, padded with '0'
            stm << std::setfill('0') << std::setw(5) << number ;

            const std::string str_number = stm.str() ; // get a copy of  the string that was written into 
Last edited on
If you will update you MinGW then maybe your compiler will like to_string()

http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g

http://tehsausage.com/mingw-to-string

and about what TooExplosive said I think she/he is trying to say something like this
1
2
3
4
5
6
7
8
char num[5],t[5] = {'0', '0', '0', '0', '0'};

cin>>num;
int i=strlen(num);
 for(int j=5-i;j<i;j++)
{
t[j]=num[j];
}


but using char num[5] is not good IMO, there is a set of str() functions which are way better

http://www.cplusplus.com/reference/string/string/

EDIT: like JLB did it



Last edited on
closed account (48T7M4Gy)
Another way using the array of 5 elements is to have an array of integers initialized to zero ( = {0} ).

Simply decompose the input integer by modular arithmetic base 10 into separate integers (0-9) in the array.

This array is then the basis for a palindrome algorithm.

Obviously 5 elements limit the size of the input.

Also since you are checking for palindromes, the sequence, but not the order of elements is important because the array is checked in both directions.

For those not enamored to arrays use vectors etc. But arrays are simple, ideal and quite workable.
Topic archived. No new replies allowed.