Search code examples
c++stringfunctionioruntime-error

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n error


enter image description hereI am trying to take multiple test cases input of a string and then test my code for printing the longest palindrome in the provided string, my logic works perfectly well but this part of multiple test cases in the main() function is throwing the following error

terminate called after throwing an instance of 'std::out_of_range'   what():  basic_string::at: __n error

I tried to debug my program and found that when I call longestPalin() function in the main() function, it is throwing an error. Please tell me what's wrong there

#include <iostream>
#include <string>
using namespace std;
bool isPalin(string s, int k, int j)
{
    bool isPalindrome = true;
    for (int i = k; i < k + ((j - i + 1) / 2); i++)
    {
        if (s.at(i) != s.at(j - i + 1))
        {
            isPalindrome = false;
            return isPalindrome;
        }
    }
    return isPalindrome;
}
string longestPalin(string s)
{
    string str;
    int i, j;
    i = 0;
    j = s.size() - 1;
    while (i < j)
    {
        if (s.at(i) != s.at(j))
        {
            j--;
        }
        else
        {
            if (isPalin(s, i, j))
            {
                str = s.substr(i, j - i + 1);
            }
            else
            {
                i++;
                j = s.size() - 1;
            }
        }
    }
    return str;
}
int main()
{
    int T;
    string s;
    cin >> T;
    for (int i = 0; i < T; i++)
    {
        cout << "enter a string" << endl;
        cin >> s;
        cout << "string is taken" << endl;
        longestPalin(s);// this part is throwing error
    }
    return 0;
}

this line is causing error and you can see that s.at(i) is working , it means that s is accessible and it is not giving out of bound error


Solution

  • Your error is at the line 9. When you are doing:

    if (s.at(i) != s.at(j - i ))
    

    then s.at(j - i + 1) is causing a std::out_of_range exception. When the starting letter of your string and the endinf letter is the same, it goes through this code and it is out of range.

    For instance, if your input is as follows:

    aaabba
    

    you will have j - i + 1 will be equal to 6 at the first run of the loop (for k equals to 0 and j equals to 5, however, you cannot have s.at(6) as your string has a size of

    This will happen only if your input has the first letter similar to its last letter.

    That doesn't mean that there won't be any other problem. I have seen for instance that if you correct this code to be "in-range", you still have an infinite loop that comes after it.

    To help you see the problem, either use gdb or any other debugger and go through it line by line or at least debug by printing some messages and values to allow you to track invalid values (like the following simple debug messages - which is not ideal but better than nothing imho).

    I suggest you a good 20-min gdb tutorial here

    bool isPalin(string s, int k, int j)
    {
        bool isPalindrome = true;
        for (int i = k; i < k + ((j - i + 1) / 2); i++)
        {
            //put breakpoint here or some debug messages
            std::cout << "j = " << j << " and j - i + 1 = " << j - i + 1 << std::endl;
            std::cout << "problem is not here "<<s.at(i) << " but here " << s.at(j - i + 1) << std::endl;
            if (s.at(i) != s.at( j - i +1))
            {
                isPalindrome = false;
                return isPalindrome;
            }
        }
        return isPalindrome;
    }