| | |
Permutations of a String
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 21
Reputation:
Solved Threads: 1
This is the code to print the Permutations of a String.
C++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<string.h> void permute(char *str,int l,int pos,int r); void swap(char &a,char &b); void print_string(char *str,int r); int main() { char str[10]=""; int l,r; printf("Enter The String : "); gets(str); l=strlen(str); printf("Enter The Number Of Places To permute on : "); scanf("%d",&r); printf("The Following Permuations are possible : \n\n"); permute(str,l,1,r); getch(); return 0; } void permute(char *str,int l,int pos,int r) { //If lock position is on the next character //than the limit if(pos==r+1) { print_string(str,r); //print - these are the elements// printf(" "); return; //and return// } //true subscript of character in array is pos-1// for(int i=pos-1;i<=l-1;i++) { //swap the first letter with all next letters str[pos-1]=str[pos-1]+str[i]-(str[i]=str[pos-1]); permute(str,l,pos+1,r); //restore the swap{swap : a=a+b-(b=a)} str[pos-1]=str[pos-1]+str[i]-(str[i]=str[pos-1]); } } void print_string(char *str,int r) { for(int i=0;i<r;i++) printf("%c",str[i]); }
Last edited by theausum; Oct 28th, 2008 at 1:16 am.
![]() |
Similar Threads
- permutation of a string in c++ (C++)
- permutations of STRING Vs First n natural numbers (C++)
- How to generate a list of permutations for a given binary number (Java)
- how to generate substrings (order matters) from an input string?? (Python)
- Producing Power set (C++)
Other Threads in the C++ Forum
- Previous Thread: can't see output although it has no error while compiling
- Next Thread: C++ Function Homework
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






