Feb 13, 2009 at 4:39pm UTC
may i know how to swap letter in string
str.swap(str2) is swapping whole string ...i wan swap letter oni..can anyone teach me.
Feb 13, 2009 at 5:44pm UTC
1 2 3 4 5
#include <algorithm>
//...
string s1 = "a" ;
string s2 = "b" ;
iter_swap( s1.begin(), s2.begin() );
Or even:
1 2 3 4 5
string s1 = "a" ;
string s2 = "b" ;
char temp = s1[0];
s1[0] = s2[0];
s2[0] = temp;
Last edited on Feb 13, 2009 at 5:48pm UTC
Feb 13, 2009 at 6:22pm UTC
hmm..but how if string sl = "ball";
and i want to swap the letter inside that string... i mean "ball" become "allb" or "llba" and so on..almost until find all posibilities..
is it possible to do so.
Last edited on Feb 13, 2009 at 6:23pm UTC
Feb 13, 2009 at 7:19pm UTC
for int it is working great but when i put a string or array, the thing doest work... is it my code is wrong...
// next_permutation
#include <iostream>
#include <algorithm>
#include <conio>
#include <string>
using namespace std;
int main () {
string myints[5];
cout<<"enter a word:";
cin>>myints[5];
cout << "The 3! possible permutations with 3 elements:\n";
sort (myints,myints+3);
do {
cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
} while ( next_permutation (myints,myints+3) );
getch();
return 0;
}
this one got error..can compile but cant run.
Feb 13, 2009 at 7:33pm UTC
cin >> myints[5]
5 is not an accessible array element. C++ works from a zero base so the largest possible value would be a 4.
Feb 13, 2009 at 7:40pm UTC
oic.. its a new thing for me. thanks alot.. but i still didnt manage to get wat i realy wan. i put 4, but the screen show empty result
Feb 13, 2009 at 8:10pm UTC
Why are you using a string array?
1 2 3 4 5
string myints;//In your code you are using just 1 string, not an array
cin >> myints;// if you want to get spaces use getline(cin,myints)
sort ( myints.begin(), myints.end() );//STL algorithms work with iterators
Last edited on Feb 13, 2009 at 8:11pm UTC