Created
December 21, 2013 20:19
-
-
Save Zulqurnain/5cb9e14b16fe69e4d8c7 to your computer and use it in GitHub Desktop.
Take Any String and save its permutations in a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string.h> | |
| #include <iostream> | |
| #include <fstream> | |
| using namespace std; | |
| class Permute{ | |
| int start,end,count; | |
| char *f,*str; | |
| public: | |
| Permute(char* st,char* fn){ // fn = file name / full path with name | |
| start=0; | |
| end=strlen(st)-1; | |
| f=new char [strlen(fn)+1]; | |
| strcpy(f,fn); | |
| str=new char [end]; | |
| strcpy(str,st); | |
| count=0; | |
| //Worker(st,start,end); | |
| } | |
| void setstart(int s){ // set starting position | |
| start=s-1; | |
| } | |
| void setend(int e){ // set ending position | |
| end=e-1; | |
| } | |
| void Calcuate(void){ | |
| cout<<"\n:: Creating Permutations of word "<<str<<" ::\n"; | |
| cout<<"Processiong Entries Saved "; | |
| Worker(str,start,end); | |
| cout<<count; | |
| cout<<"\n Permutation are Saved in file "<<f<<"\n"; | |
| } | |
| void swap(char *fir, char *sec){ // swap characters of char array/string | |
| char temp = *fir; | |
| *fir = *sec; | |
| *sec = temp; | |
| } | |
| /* it is Basically Solved By Backtracking check this link | |
| it is very high level technique with simple logic , 1 or more character will remains same and rest | |
| will iterate and swap, | |
| "IT IS DONE BY RECURSION !" , if abcd is converted to abdc the it will backtrack it as abcd again !! | |
| same as Push/Pop in stack , it is specially written for Habeel and SAAD | |
| */ | |
| void Worker(char *a, int s, int e) { // | |
| int it; //iterator | |
| if (s == e){ | |
| Saveit(a); | |
| } | |
| else{ | |
| for (it = s; it <= e; it++){ | |
| swap((&a[s]), (&a[it])); | |
| Worker(a, s+1, e); | |
| swap((&a[s]), (&a[it])); //backtrack | |
| } | |
| } | |
| } | |
| void Saveit(char* arr){ // saving string to file | |
| ofstream out; | |
| out.open(f,ios::app); | |
| out<<arr<<"\n"; | |
| out.close(); | |
| /*system("CLS"); | |
| cout<<"Processiong Entries Saved "; | |
| count++; | |
| cout<<count;*/ | |
| count++; | |
| } | |
| ~Permute(void){ | |
| delete [] f; | |
| } | |
| }; | |
| int main(){ // Written By : Zulqurnain jutt | |
| char t[] = "ABC"; | |
| Permute p(t,"Permute.txt"); | |
| p.Calcuate(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment