3

I want to reverse a string without the use of a loop. My code with the loop looks like:

#include <iostream>
#include <string>

using namespace std;

string reverseString(string str) {
    string changedString;
    int strLength = int(str.length() - 1);
    for(int i {strLength}; i >= 0; i--) {
        changedString.push_back(str.at(i));
    }
    return changedString;
}

int main() {

    string str;

    cout << "Enter a string to reverse it:\n" << flush;
    cin >> str;
    cout << reverseString(str) << flush;
} 

Now I need to write a function without the loop. Only the methods of String should be used. Can you help me solving this problem?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
tiq
  • 43
  • 4
  • Does this answer your question? [How to reverse an std::string?](https://stackoverflow.com/questions/4951796/how-to-reverse-an-stdstring) – Daniel Langr Nov 26 '19 at 11:20
  • btw as you pass the string by value, you could store the reversed in the parameter itself and remove the `changedString`, alternatively pass by reference to avoid the unnecessary copy – 463035818_is_not_an_ai Nov 26 '19 at 11:20
  • Does this answer your question? [How do you reverse a string in place in C or C++?](https://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – Adrian Mole Nov 26 '19 at 11:31
  • In order to repeat a process you are going to need a loop, whether you explicitly write one or you use a function that has a loop. Even recursion can be considered a loop. – Thomas Matthews Nov 26 '19 at 16:16

3 Answers3

8

It is very simple to write such a function

std::string reverse( const std::string &s )
{
    return { s.rbegin(), s.rend() };
}

Here is a demonstrative program

#include <iostream>
#include <string>

std::string reverse( const std::string &s )
{
    return { s.rbegin(), s.rend() };
}

int main() 
{
    std::string s( "Hello World" );

    std::cout << s << '\n';
    std::cout << reverse( s ) << '\n';

    return 0;
}

Its output is

Hello World
dlroW olleH
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

Well, you can do that using recursion. Here are some links if you aren't aware what recursion is : link1 and link2. Technically it won't be a loop.

string reverseString(string str, int index, string ans) {
    if (index == -1) return ans;
    ans += str[index];
    return reverseString(str, index - 1, ans);
}

Parameters for this function will be str as it was by default, index = size(str) - 1 and ans ans = ""; reverseString(str, size(str) - 1, "") for example.

If you want your function to take exactly one argument, then you can write wrapper function and the one I wrote will have different name - reverseStringWrapper for example and in reverseString there will be only one line - return reverseStringWrapper(str, size(str) - 1, "");

string reverseStringWrapper(string str, int index, string ans) {
    if (index == -1) return ans;
    ans += str[index];
    return reverseString(str, index - 1, ans);
}

string reverseString(string str) {
    return reverseStringWrapper(str, size(str) - 1, "");
}
gjiki
  • 169
  • 1
  • 6
  • 1
    The same could be achieved with one function and the appropriate default values in 2nd and 3rd argument. (Maybe, with start `index = 0` counting from end of `str`.) – Scheff's Cat Nov 26 '19 at 12:10
1

How was this?

  • In c, You can use strrev() function to reverse the string(char*)
  • In c++, you can either use std::reverse() or StringBuilder.reverse() method to reverse a string.

. This way you can reverse the char array(char*).

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

// Function to reverse a given character array using std::reverse
void reverse(char *str)
{
    std::reverse(str, str + strlen(str));
}

// main function
int main()
{
    /* using C string */
    char s[] = "Hello World";

    reverse(s);
    cout << "Reverse of the given string is : " << s;

    return 0;
}

This way you can reverse the string.

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

// Function to reverse a given character array using std::reverse
void reverse(char* str)
{
    std::reverse(str, str + strlen(str));
}

// main function
int main()
{
    /* using C string */
    // char s[] = "Techie Delight";
    string s = "hello world";
    int n = s.length(); 

    // declaring character array 
    char char_array[n + 1]; 

    // copying the contents of the 
    // string to char array 
    strcpy(char_array, s.c_str()); 
    reverse(char_array);
    s = char_array;
    cout << "Reverse of the given string is : " << s;

    return 0;
}

Hope this might Helps:)

VJAYSLN
  • 473
  • 4
  • 12
  • The usage of `std::reverse` was already mentioned in [the other answer](https://stackoverflow.com/a/59049714/7478597) (and even without the needless copying of `std::string` to `char[]`). Btw. [VLA](https://en.wikipedia.org/wiki/Variable-length_array)s are not part of C++ standard. And, why do you mention [tag:c]? The question is tagged [tag:c++] (and only [tag:c++]). – Scheff's Cat Nov 26 '19 at 12:14
  • @Scheff There is no answers was mentioned the usage of `std::reverse` . This is different answer from other answer. – VJAYSLN Nov 26 '19 at 12:19
  • Concerning, `std::reverse()` you are right - my fault. (I overlooked that it works even without `std::reverse()`.) However, the rest is still true. – Scheff's Cat Nov 26 '19 at 12:21