Sinisterly
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)

Pages: 1 2 3


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?
Code:
acb a c bca b c cab c a
here is full c++ program
Code:
#include<iostream> #include<string> void swap(char* src,char * dest){ char ch=*src; *src=*dest; *dest=ch; } void permutation(std::string str,int start,int end){ if(end-start==1) std::cout<<str<<std::endl; else{ for(int i=start;i<end;i++){ swap(&str[start],&str[start+i]); permutation(str,start+1,end); swap(&str[start],&str[start+i]); } } } int main(){ std::string str; std::cout<<"enter any string = "; std::cin>>str; std::cout<<"you entered = "<<str<<std::endl; std::cout<<"length of string = "<<str.length()<<std::endl; permutation(str,0,str.length()); return 0; }

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?
Code:
acb a c bca b c cab c a
here is full c++ program
Code:
#include<iostream> #include<string> void swap(char* src,char * dest){ char ch=*src; *src=*dest; *dest=ch; } void permutation(std::string str,int start,int end){ if(end-start==1) std::cout<<str<<std::endl; else{ for(int i=start;i<end;i++){ swap(&str[start],&str[start+i]); permutation(str,start+1,end); swap(&str[start],&str[start+i]); } } } int main(){ std::string str; std::cout<<"enter any string = "; std::cin>>str; std::cout<<"you entered = "<<str<<std::endl; std::cout<<"length of string = "<<str.length()<<std::endl; permutation(str,0,str.length()); return 0; }

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; }