Finding next palindrome of a number, getting SIGABRT error

  c++, palindrome, runtime-error, string

Hi ,I am trying to solve the question PALIN on SPOJ where the idea is to find the next immediate palindrome of a number. I tested my code on the IDE with all possible test cases I found and the program is working fine but I can’t figure out why I am getting SIGABRT error when I submit the solution.Please I request someone help me figure out the problem in the code For yor reference I am providing the link of the question:- https://www.spoj.com/problems/PALIN/

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;

bool all_nines(string s)
{
    for(int i=0;i<s.length();i++)
    {
        if(s[i]!='9')
            return false;
    }
    return true;
}

int reverse(int a)
{
    int rev=0;
    while(a)
    {
        rev=(rev*10)+(a%10);
        a/=10;
    }
    return rev;
}

void convert_and_compare(string &s,int low,int high)
{
    int low_num,high_num;
    string low_str="",high_str="";
    for(int i=0,j=high;i<=low,j<s.size();i++,j++)
    {
        low_str+=s[i];
        high_str+=s[j];
    }
    low_num=stoi(low_str);
    high_num=stoi(high_str);
    if(reverse(low_num)<=high_num)
    {
        low_num++;
        low_str=to_string(low_num);
        high_str="";
        for(int i=low_str.length()-1;i>=0;i--)
            high_str+=low_str[i];
    }
    else
    {
        high_str="";
        for(int i=low_str.length()-1;i>=0;i--)
            high_str+=low_str[i];
    }
    s="";
    s+=low_str+high_str;
}

void convert_and_compare(string &s,int low,int high,int mid)
{
    int low_num,high_num,mid_num;
    string low_str="",high_str="";
    char mid_str;
    for(int i=0,j=high;i<=low,j<s.size();i++,j++)
    {
        low_str+=s[i];
        high_str+=s[j];
    }
    low_num=stoi(low_str);
    high_num=stoi(high_str);
    mid_num=(s[mid]-'0');

    if(reverse(low_num)>high_num)
    {
        
        low_str=to_string(low_num);
        high_str="";
        for(int i=low_str.length()-1;i>=0;i--)
            high_str+=low_str[i];
        mid_str=mid_num+'0';
    }
    else if(reverse(low_num)<=high_num && mid_num!=9)
    {
        mid_num++;
        high_str="";
        for(int i=low_str.length()-1;i>=0;i--)
            high_str+=low_str[i];
        mid_str=mid_num+'0';
    }
    else if(reverse(low_num)<=high_num && mid_num==9)
    {
        mid_num=0;
        low_num++;
        low_str=to_string(low_num);
        high_str="";
        for(int i=low_str.length()-1;i>=0;i--)
            high_str+=low_str[i];
        mid_str=mid_num+'0';
    }
    s="";
    s=low_str+mid_str+high_str;
}

string find_next_palin(string s)
{
    if(all_nines(s))
    {
        s[0]='1';
        for(int i=1;i<s.length();i++)
            s[i]='0';
        s+='1';
        return s;
    }
    else
    {
        if(s.length()%2==0)
        {
            int low,high;
            low=s.length()/2-1;
            high=s.length()/2;
            convert_and_compare(s,low,high);
            return s;
        }   
        else
        {
            int low,high,mid;
            mid=s.length()/2;
            low=mid-1;
            high=mid+1;
            convert_and_compare(s,low,high,mid);
            return s;
        }          
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    vector <string> v(t);
    for(int i=0;i<t;i++)
    {
        cin>>v[i];
    }
    for(int i=0;i<t;i++)
    {
        if(v[i].length()==1)
        {
            if(v[i]=="9")
            {   printf("11");
                printf("n");
            }
            else
            {    
                printf("%d",(v[i][0]-'0')+1);
                printf("n");
            }
            
        }
        else
        {
            string temp;
            temp=find_next_palin(v[i]);
            for(int j=0;j<temp.length();j++)
                cout<<temp[j];
                printf("n");
        }
    }
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT