The Next Palindromic number I have incremented an integer in the function nextpalin() and converted it into a std::string for finding whether it's a palindrome or not. 

If it is a palindrome I return it. The code is giving TLE on SPOJ but working fine on other online IDEs; how to make this code more efficient?

#include &lt;iostream&gt;
#include &lt;string.h&gt;
using namespace std;
int nextpalin (int num){
    while (num++) {
        string str = to_string (num); /// int to string conversion
        int l = str.length()-1;
        int s = 0;
        while( s&lt;l ){
              if (str[s]!=str[l]) break;
              else {
                    s++;
                    l--;
                    }
        if (s&gt;=l) return num;}
                }
   }

int main () {
    int t;
    cin &gt;&gt; t;
    while (t--){
    int num;
    cin &gt;&gt; num;
    if (num==0)  cout &lt;&lt; "1" &lt;&lt; endl;
   else { 
       if (num&lt;9)  cout &lt;&lt; num+1 &lt;&lt; endl;
       else  cout &lt;&lt; nextpalin( num) &lt;&lt; endl;}
     }
}
Well, my first impression is that your formatting is broken. Generally, before you even consider checking in a new revision to your local source-control or showing it to anyone, the code should be properly formatted, preferably automatically.
Importing big / open-ended namespaces is a bad idea. Read "Why is “using namespace std;” considered bad practice?" for the reasons.
You are missing the header for std::string. The unused header for C-Strings does not make up for that omission.
nextpalin() is quite a non-obvious Abbreviation, nor does it follow any of the conventions for multi-word-names. Why abbreviate at all?
Mark functions and variables only used in the local TU as TU-local using static. That reduces chances for conflicts and encourages inlining.
nextpalin() uses brute-force without any consideration for efficiency. Work smarter not harder, and you might avoid TLE.
Consider using an unsigned type when a negative number makes no sense. Can your input be negative? I doubt it, but you should know better.
Don't arbitrarily restrict the domain of your function without good reason, and document it if you do. Specifically, why doesn't it handle zero, and what business does main() have dealing with it instead?
Always check whether input succeeded. There are any number of ways it could fail.


First, there are smarter ways to check whether a given number is a palindrome in some arbitrary base than conversion to string. See "Palindrome numbers - Project Euler Problem 4". Not that it matters, because incrementing and testing is incredibly inefficient already.

Next, there are smarter ways to go from a number to the next one which is a palindrome than just checking one after the other. One possible algorithm:


n &lt; 0 || n >= max_palindrome => 0
Increment n.
Take the upper half of the number (including middle if odd size).
If it is smaller than the reversed lower half (including middle if odd size), increment it.
Re-generate the bottom half from the top half to get the full number you wanted.


Finding the biggest palindrome, necessary for wrap-around and avoiding overflow:

template &lt;unsigned long long base = 10, class T&gt;
constexpr bool is_power(T n) {
    static_assert(base &gt;= 2, "The base must be at least 2.");
    constexpr T base = BASE;
    if (n &gt; 0)
        while (!(n % base))
            n /= base;
    return n == 1;
}

template &lt;unsigned long long BASE = 10, class T&gt;
constexpr T last_palindrome(T n) {
    static_assert(base &gt;= 2, "The base must be at least 2.");
    constexpr T base = BASE;

    n = n &gt; 0 ? n - 1 : std::numeric_limits&lt;T&gt;::max();
    if (is_power&lt;BASE&gt;(n))
        return n - 1;

    T top = n, rbottom = 0;
    for (; n &gt;= base; n /= base * base) {
        rbottom = rbottom * base + top % base;
        top /= base;
    }
    if (n)
        rbottom = rbottom * base + top % base;

    top -= top &gt; rbottom;

    for (n = n ? top / base : top; n; n /= base)
        top = top * base + n % base;
    return top;
}


An implementation of the algorithm itself:

template &lt;unsigned long long BASE = 10, class T&gt;
constexpr T next_palindrome(T n) {
    static_assert(base &gt;= 2, "The base must be at least 2.");
    constexpr T base = BASE;
    constexpr T biggest = last_palindrome&lt;BASE&gt;(T());

    if (n &lt; 0 || biggest &gt; 0 &amp;&amp; n &gt;= biggest)
        return 0;
    ++n;

    T top = n, rbottom = 0;
    for (; n &gt;= base; n /= base * base) {
        rbottom = rbottom * base + top % base;
        top /= base;
    }
    if (n)
        rbottom = rbottom * base + top % base;

    top += top &lt; rbottom;

    for (n = n ? top / base : top; n; n /= base)
        top = top * base + n % base;
    return top;
}