Check if palindrome

This sode just seems to return true always

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

using namespace std;
vector<int> digits ;

void digitExtract(int x)            //extracts digits
{
        while(x>0)
    {
        digits.push_back(x%10) ;
        x = x/10 ;
    }

}
bool ispalindrome(int x )                  //checks for palindrome
{   digitExtract(x) ;
    int size = digits.size() ;
    for(int i=0 ; i < size/2  ;i++)
       {
           if(!digits[i]==digits[size-1-i])
           {
              return false ;
           }
       } return true ;
}

int main()
{

    int x ;
    cout << "Enter a Number : " ;
    cin>> x ;
    cout << ispalindrome(x)<< endl;
   
}
Last edited on
what are you having problems with? From your code, I don't see any use for the while (1) never-ending loop. It would also return a wrong answer for subsequent tries because you are not "re-setting" the digits vector to zero after every iteration.

Be careful of the boundary conditions also.

Also, you are not returning anything from the main function.
i removed the while loop it wasnt the problem though....
i use code blocks so no need to return a value in main function
i checked the boundary conditions but it just keeps returning true for any number
Last edited on
change
if (!digits[i]==digits[size-1-i])
to
if (digits[i]!=digits[size-1-i])

your original "!" applied only to digits[i], not the entire condition
OK ty
i always seem to make such stupid errors
Topic archived. No new replies allowed.