In this blog we will learn How to reverse a const string in c++.  In normal case , i mean when string is not constant we can use standard c++ std::reverse() function. For example let us take an example of normal string reverse.

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

int main()
{
        using namespace std;
        string s("ilkop");

        reverse(s.begin(),s.end());

        cout<<s<<endl;

        return 0;
}

Now let make string as const string and try to compile sample program.

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

int main()
{
        using namespace std;
        const string s("ilkop");

        reverse(s.begin(),s.end());

        cout<<s<<endl;

        return 0;
}

The above program will fail at compilation time with allot of errors. The error will come as we are attempting to reverse a const string. so what?  Let us look into the error log.

In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from main.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’:
/usr/include/c++/7/bits/stl_algo.h:1160:18:   required from ‘void std::__reverse(_RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’
/usr/include/c++/7/bits/stl_algo.h:1186:21:   required from ‘void std::reverse(_BIter, _BIter) [with _BIter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’
main.cpp:10:27:   required from here
/usr/include/c++/7/bits/stl_algobase.h:148:11: error: no matching function for call to ‘swap(const char&, const char&)’
       swap(*__a, *__b);
       ~~~~^~~~~~~~~~~~

it is a clear indication that we can not reverse a const string using std::reverse() function. In this case we can read const string from end to start index , and make a new string. The new string would be reverse of const string.

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

int main()
{
    using namespace std;
    const string s("ilkop");

    // find length of string s
    int strLen = s.length();
    // allocate memory for  new string of same lenght
    char *tmp = new char[strLen];
    int j=0;
    for(int i = strLen - 1; i >= 0; i--)
    {
      tmp[j++] = s.at(i);
    }
    // append null char
    tmp[j]='\0';

    cout << tmp << endl;
        // delete memory
    delete [] tmp;
    return 0;
}

Ref:

https://codereview.stackexchange.com/questions/135123/program-to-reverse-a-string-using-stdstring

Read free course from



Related Contents to follow