C++ string swap character places

RnD picture RnD · Nov 19, 2011 · Viewed 49.1k times · Source

is there a way to swap character places in a string? For example if I have "03/02" I need to get "02/03". Any help is appreciated!

Answer

Kerrek SB picture Kerrek SB · Nov 19, 2011

Sure:

#include <string>
#include <algorithm>

std::string s = "03/02";
std::swap(s[1], s[4]);