How to Return a String From a Function in C++

  1. Use the std::string func() Notation to Return String From Function in C++
  2. Use the std::string &func() Notation to Return String From Function
  3. Use the char *func() Notation to Return String From Function

This article explains several methods on how you can return a string from a function in C++.

Use the std::string func() Notation to Return String From Function in C++

Return by the value is the preferred method for returning string objects from functions. Since the std::string class has the move constructor, returning even the long strings by value is efficient. If an object has a move constructor, it’s said to be characterized with move-semantics. Move-semantics imply that the object is not copied to a different location on function return, thus, providing faster function execution time.

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

using std::cout; using std::endl;
using std::string; using std::reverse;

string ReverseString(string &s){
    string rev(s.rbegin(), s.rend());
    return rev;
}

int main() {
    string str = "This string shall be reversed";

    cout << str << endl;
    cout << ReverseString(str) << endl;

    return EXIT_SUCCESS;
}

Output:

This string shall be reversed
desrever eb llahs gnirts sihT

Use the std::string &func() Notation to Return String From Function

This method uses return by reference notation, which can be an alternative approach to this problem. Even though return by reference is the most efficient way to return large structures or classes, it would not impose extra overhead compared to the previous method in this case. Take note that you shouldn’t replace a local variable declared in the function by a reference; this leads to a dangling reference.

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

using std::cout; using std::endl;
using std::string; using std::reverse;

string &ReverseString(string &s) {
    reverse(s.begin(), s.end());
    return s;
}

int main() {
    string str = "Let this string be reversed";

    cout << str << endl;
    cout << ReverseString(str) << endl;

    return EXIT_SUCCESS;
}

Output:

Let this string be reversed
desrever eb gnirts siht teL

Use the char *func() Notation to Return String From Function

Alternatively, we can use char * to return a string object from a function. Remember that the std::string class stores characters as a continuous array. Thus, we can return a pointer to the first char element of that array by calling the built-in data() method. However, make sure you don’t to use a similar c_str() method when returning null-terminated character array of std::string object, as it replaces the const pointer to the first char element.

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

using std::cout; using std::endl;
using std::string; using std::reverse;

char *ReverseString(string &s) {
    reverse(s.begin(), s.end());
    return s.data();
}

int main() {
    string str = "This string must be reversed";

    cout << str << endl;
    cout << ReverseString(str) << endl;

    return EXIT_SUCCESS;
}

Output:

This string must be reversed
desrever eb tsum gnirts sihT

Related Article - C++ String

  • How to Convert String to Uppercase in C++
  • How to Add Int to String in C++