Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

Getting Runtime Exception (SIGSEV)

$
0
0

I dont know what's wrong with this code. Please help

#include<iostream>
#include<cstring>
using namespace std;
string str;
void increment(long j) {
    if(j<0) {
        str = '1' + str;
        return ;
    }
    if(str.at(j) =='9') {
        str.at(j) = '0';
        increment(j-1);
    }
    else str.at(j) = (char)((int)str.at(j) + 1);
}
void calc() {

    cin>>str;
    if(str == string(str.rbegin(),str.rend())) {
        increment(str.length()-1);
    }
    long j=str.length()-1;

    long i=0;
    while(i<j) {
        if(str.at(i) < str.at(j)) {
            increment(j-1);
        }
        str.at(j) = str.at(i);
        i++;j--;
    }
    cout<<str<<endl;
}
int main() 
{
    str = "";
    int T;

    cin>>T;
    for(int i=0;i<T;i++) calc();
    return 0;
}

Viewing all articles
Browse latest Browse all 39796


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>