'Reversing a string. Element versus whole string and without built-in functions
I'm doing an exercise where I need to reverse the elements of a string e.g. HelloWorld - dlroWolleH
My original code was here as below:
std::string reverseString(std::string text)
{
std::string new_string = "";
int index = 0;
int pos_text = text.size();
while (index <= text.size()) {
new_string[index] = text[pos_text]; //Line doesn't work.
pos_text--;
index++;
}
return new_string;
}
However, looking online, the code block inside the while loop goes like this instead.
new_string = new_string + text[pos_text];
Could someone explain to me why it needed to be as the line above? I was under the impression that a string is an array of characters?
Solution 1:[1]
Could someone explain to me why it needed to be as the line above? I was under the impression that a string is an array of characters?
Actually, strings are dynamic arrays who's size is not fixed. They have the + operator overloaded so you can append two strings using str1 += str2. For example, "hello" + "world" == "helloworld"
When you create new_string, you create an empty string so you cannot access any of its elements, because it has none. That's why you need to use +=, to append an element.
Note: In practice, you should use std::reverse(string.begin(), string.end()) instead of a custom algorithm. See here: https://en.cppreference.com/w/cpp/algorithm/reverse
Solution 2:[2]
You declared an empty string
std::string new_string = "";
You could equivalently write
std::string new_string;
So you may not use the subscript operator for an empty string to change its content
new_string[index] = text[pos_text];
Also in the right operand of the assignment there is also used an invalid index expression pos_text. You have to use the expression pos_text - 1. Otherwise the character '\0' will be written in the first position of the new string.
Instead you could write
new_string += text[pos_text - 1];
or (as the same )
new_string.push_back( text[pos_text - 1] );
As for this statement
new_string = new_string + text[pos_text];
that again must be written like
new_string = new_string + text[pos_text - 1];
then for the class std::string there is defined the overloaded operator + with the right operand of the ty[e char.
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
Pay attention to that the condition of the while loop
while (index <= text.size())
is incorrect. You have to write
while (index < text.size())
The simplest way to define the function is the following
std::string reverseString( const std::string &text )
{
return { text.rbegin(), text.rend() };
}
If to use loops as you are doing then the function can be defined like
std::string reverseString( const std::string &text )
{
std::string new_string;
new_string.reserve( text.size() );
for ( auto i = text.size(); i != 0; )
{
new_string += text[--i];
}
return new_string;
}
Solution 3:[3]
When you use the + sign in strings, it appends the character to the end of that string. We call this in programming "Operator Overloading".
Example:
string word1 = "Hello";
string word2 = "World";
word1 = word1 + word2;
The word1 here becomes HelloWorld.
There are diffrent ways to type this word1 = word1 + word2 line of code, looking online you can see it as word1 += word2, This also works and gives you the same result.
Here is a solution for reversing a word too:
string reverseString(string word)
{
int size = word.length();
string revWord;
for(int i = 0; i < size; i++)
{
revWord += word[size-i-1];
}
return revWord;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ted Lyngmo |
| Solution 2 | |
| Solution 3 | Ethan Brown |
