Reputation: 11
I've been doing some C++ challenges to get into coding, and I came across this when I tried to create a function that reversed a string. for some reason assigning result[i] to str[str.length() - i] doesn't work, but assigning str[str.length() - i] to holding and then holding to result[i] does. Can anyone explain this?
//this works
std::string reverse(std::string str){
std::string result;
char holding;
for(uint i = 0; i <= str.length(); i++){
holding = str[str.length() - i];
result += holding;
}
return result;
}
//this doesn't
std::string reverse(std::string str){
std::string result;
for(uint i = 0; i <= str.length(); i++){
result[i] = str[str.length() - i];
}
return result;
}
Upvotes: 1
Views: 234
Reputation: 308
The second snippet of code is not working because the string has just declared and only the default constructor is called on that string, and according to documentation, the default size of the string is 0, due to which, when you are writing this lineresult[i] = str[str.length() - i];
(correct line should be
result[i] = str[str.length() - i - 1];
)
You will receive an out of index error which is obvious because the string size is 0.
see this.
working code->
std::string reverse(std::string str){
std::string result(str);
for(uint i = 0; i < str.length(); i++){
result[i] = str[str.length() - i-1];
}
return result;
}
Upvotes: -1
Reputation: 29
The lenght of result
is 0. please use resize
fuction or like this:
std::string reverse(std::string str){
std::string result = str; // temp value
for(uint i = 0; i < str.length(); i++){
result[i] = str[str.length() - i - 1];
}
return result;
}
Upvotes: 0
Reputation: 1037
Am assuming you are coming from a language like javascript where you can assign any index of an array and the system will aquire memory for it in the background. C++ is much less free in that regard. You either have to give the string a size (so it reserves memory) to assign data later to each index, or use some method that extends the string (such a method is called when doing += on it). The later case re-aquires fresh memory every time by the way and copies the contained characters over which is not ideal from a performance viewpoint.
Upvotes: 0
Reputation: 238401
result
is an empty string. Assigning anything other than result[0] = '\0'
will have undefined behaviour.
but assigning str[str.length() - i] to holding and then holding to result[i] does.
You aren't assigning to result[i]
in the working example. It works because instead of assigning a non-existing character, you use the compound assignment operator +=
which performs concatenation into the end of the string.
P.S. There is a function in the standard library for reversal. Or you could just use reverse iterators.
Upvotes: 3
Reputation: 16447
The size of result
is 0. Accessing result[i]
causes undefined behavior in nearly all cases. You can preallocate the needed memory with
std::string reverse(std::string str){
std::string result(str.length(), '\0');
for(uint i = 0; i < str.length(); i++){
result[i] = str[str.length() - i - 1];
}
return result;
}
Upvotes: 3