| | |
Permutations of a String
![]() |
•
•
Join Date: Mar 2008
Posts: 21
Reputation:
Solved Threads: 1
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 Ancient Dragon; Oct 27th, 2008 at 7:37 am. Reason: add code tags
![]() |
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: Quick Random Question
- Next Thread: Segmentation fault C
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





