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 - Psycho_Coder - 05-01-2014

Fixed a few things and edited buggy links.


RE: String Permutations - Ex094 - 05-01-2014

You can also use next_permutation() but Nice work Biggrin


RE: String Permutations - Psycho_Coder - 05-01-2014

(05-01-2014, 02:13 PM)Ex094 Wrote: You can also use next_permutation() but Nice work Biggrin



Well that is an in-built function and this a tutorial about how to generate permutations through code since all programming languages don't have in-built functions.


RE: String Permutations - computerbug - 08-21-2014

i tried this function but it's not giving correct output what's the problem with this function ?

Code:
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]); } } }



RE: String Permutations - computerbug - 08-21-2014

i tried this function but it's not giving correct output what's the problem with this function ?

Code:
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]); } } }



RE: String Permutations - Psycho_Coder - 08-22-2014

(08-21-2014, 03:21 PM)computerbug Wrote: i tried this function but it's not giving correct output what's the problem with this function ?

Code:
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]); } } }


What output do you get ?


RE: String Permutations - Psycho_Coder - 08-22-2014

(08-21-2014, 03:21 PM)computerbug Wrote: i tried this function but it's not giving correct output what's the problem with this function ?

Code:
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]); } } }


What output do you get ?


RE: String Permutations - Psycho_Coder - 08-22-2014

(08-21-2014, 03:21 PM)computerbug Wrote: i tried this function but it's not giving correct output what's the problem with this function ?

Code:
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]); } } }


What output do you get ?


RE: String Permutations - computerbug - 08-22-2014

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



RE: String Permutations - computerbug - 08-22-2014

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