Home > Software engineering >  terminate called after throwing an instance of 'std::length_error' what(): basic_string::_
terminate called after throwing an instance of 'std::length_error' what(): basic_string::_

Time:11-15

The problem is to reverse words in a string ...

Eg. - This is Nice
Output -Nice is This

so here's the error

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_create

Here's my actual code, don't know where it went wrong I just started c , but I'm sure I'm trying to access an index that isn't defined. Pls correct me if I'm wrong

string reverseWords(string s) {
        vector<string> v;
        string x="";
        for(int i=0;i<s.size();i  )
        {
            if(isspace(s[i]))
            {
                v.push_back(x);
                x="";
                v.push_back(" ");
                
            }
            else
            {
                x=x s[i];
            }
        }
        v.push_back(x);
        x="";
        for(int j=v.size();j>=0;j--) x=x v[j];
        return x;
    }

CodePudding user response:

Your approach is inefficient.

Also you are not reversing the source string but building a new string in the reversed order.

The compound statement of the if statement

if(isspace(s[i]))
{
    v.push_back(x);
    x="";
    v.push_back(" ");
    
}

does not make great sense when the source string contains adjacent spaces because it appends empty strings to the vector.

Also if the last character of the string is space then this statement after the for loop

v.push_back(x);

again appends a redundant empty string to the vector.

This for loop

for(int j=v.size();j>=0;j--) x=x v[j];

invokes undefined behavior when j is equal to v.size().

If you are dealing with an object of the type std::string then you should use methods of the class and standard algorithms as for example the algorithm std::reverse.

Here is a demonstration program that shows how a string can be reversed.

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

std::string & reverseWords( std::string &s, const char *delim = " \t" )
{
    auto first = s.find_first_not_of( delim );

    if (first != std::string::npos)
    {
        auto last = s.find_last_not_of( delim )   1;
        
        std::reverse( std::next( std::begin( s ), first ), std::next( std::begin( s ), last ) );

        while ( first != last )
        {
            auto pos = s.find_first_of( delim, first );

            if (pos == std::string::npos) pos = last;

            std::reverse( std::next( std::begin( s ), first ),
                std::next( std::begin( s ), pos ) );

            first = pos;

            if ( first != last ) first = s.find_first_not_of( delim, first );
        }
    }

    return s;
}

int main()
{
    std::string s( "The quick brown fox jumps over the lazy dog" );

    std::cout << s << '\n';
    std::cout << reverseWords( s ) << '\n';
}

The program output is

The quick brown fox jumps over the lazy dog
dog lazy the over jumps fox brown quick The
  • Related