![]() |
|
String Permutations - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: String Permutations (/Thread-String-Permutations) |
RE: String Permutations - ArkPhaze - 08-24-2014 (08-22-2014, 03:06 PM)computerbug Wrote: this is an output for string "abc" .I can't understand why is space there in output instead a character? You're including the null terminator into the output... RE: String Permutations - ArkPhaze - 08-24-2014 (08-22-2014, 03:06 PM)computerbug Wrote: this is an output for string "abc" .I can't understand why is space there in output instead a character? You're including the null terminator into the output... RE: String Permutations - ArkPhaze - 08-24-2014 And you incorrectly modified the original code to do something improper for string permutations... Code: #include <iostream>
#include <string>
using namespace std;
void swap(char *src, char *dst)
{
char ch = *dst;
*dst = *src;
*src = ch;
}
void permuteString(std::string s, int beg, int end)
{
int range = end - beg;
if (range == 1)
{
cout << s << endl;
return;
}
for (int i = 0; i < range; i++)
{
swap(&s[beg], &s[beg + i]);
permuteString(s, beg + 1, end);
swap(&s[beg], &s[beg + i]);
}
}
int main()
{
std::string s = "123";
permuteString(s, 0, s.length());
return 0;
}RE: String Permutations - ArkPhaze - 08-24-2014 And you incorrectly modified the original code to do something improper for string permutations... Code: #include <iostream>
#include <string>
using namespace std;
void swap(char *src, char *dst)
{
char ch = *dst;
*dst = *src;
*src = ch;
}
void permuteString(std::string s, int beg, int end)
{
int range = end - beg;
if (range == 1)
{
cout << s << endl;
return;
}
for (int i = 0; i < range; i++)
{
swap(&s[beg], &s[beg + i]);
permuteString(s, beg + 1, end);
swap(&s[beg], &s[beg + i]);
}
}
int main()
{
std::string s = "123";
permuteString(s, 0, s.length());
return 0;
} |