c++ - Is this reverse string code correct? -



c++ - Is this reverse string code correct? -

this code works me when run on ide, doesn't work on website has exercise. segmentation fault. code correct? did create mistake?

#include <iostream> #include <string> using namespace std; string firstreverse(string str) { (int = 0, = str.size()-1; != back; ++i, --back) { char c = str[back]; str[back] = str[i]; str[i] = c; } homecoming str; } int main() { cout << firstreverse("hello"); homecoming 0; }

also, what's best way this?

your index needs reach half of length, , way ensure swap between pair happens once:

for (int = 0; < str.size() / 2 ; ++) { char c = str[str.size() - 1 - i]; str[str.size() - 1 - i] = str[i]; str[i] = c;

}

c++

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -