Swapping Elements in strings.

So, I wanted to write a function which swaps elements of strings, given their place in the string, so I wrote the following code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void swap_element(string a, int q, string b, int w){
char o;
char p;
o = a[q];
p = b[w];

a[q]=p;
b[w]=o;
}

int main(){

string a, b;
cin >> a >> b;

swap_element(a, 0, b, 0);

cout<<a<<" "<<b;
}


Unfortunately it just outputs the original values of the strings. Suggestions?
Last edited on
1
2
3
4
5
#include <algorithm>

void swap_element(string& a, int q, string& b, int w) {
    std::swap(a[q], b[w]);
}


Mostly the parameter types for the strings need to be references.
Thanks. I knew I had forgotten something, but couldn't remember what :P
Topic archived. No new replies allowed.