Categories Uncategorized Write a function to print all of the permutations of a string Post author By Aaron Post date June 12, 2009 1 Comment on Write a function to print all of the permutations of a string Write a function to print all of the permutations of a string. ← Write A to I → Implement an algorithm to do wild card string matching One reply on “Write a function to print all of the permutations of a string” This is a non-recursive solution. Also kind of kludgy – I should run through this exercise again before any interviews. //---------------------------------------------------------------------------- // StringPerm: A function to take a string and print "all its permutations" //---------------------------------------------------------------------------- #include #include #include //------------------------------------------------------------ // Rotate string //------------------------------------------------------------ void rotString(int indy, char *curStr ){ char tmp; int i = 0; if( curStr[indy] == 0) return; else if( curStr[indy+1] == 0) return; else{ i = 1; tmp = curStr[indy]; while( curStr[indy+i] != 0 ){ curStr[indy+i-1] = curStr[indy+i]; i++; } curStr[indy+i-1] = tmp; } } //------------------------------------------------------------ // Generate string permutations //------------------------------------------------------------ void stringPermRec( int indy, int length, char *curString) { // Cycle through the root posabilitys. int i; if( indy < length ){ for( i = indy; i < length; i++){ rotString( indy, curString ); stringPermRec(indy+1, length, curString); } }else{ printf("%s\n", curString); } } //------------------------------------------------------------ // Wrapper for recersive calls //------------------------------------------------------------ void stringPerm( char *curString){ int length = strlen(curString); stringPermRec( 0, length, curString ); } //------------------------------------------------------------ // Main function call //------------------------------------------------------------ #define TESTSTR "1234" int main() { char *testStr = malloc( sizeof(char) * strlen(TESTSTR)); strcpy( testStr, TESTSTR); printf("----- Start -----\n"); stringPerm(testStr ); printf("----- Done -----\n"); fflush(stdout); return(0); } Leave a ReplyYou must be logged in to post a comment.
This is a non-recursive solution. Also kind of kludgy – I should run through this exercise again before any interviews. //---------------------------------------------------------------------------- // StringPerm: A function to take a string and print "all its permutations" //---------------------------------------------------------------------------- #include #include #include //------------------------------------------------------------ // Rotate string //------------------------------------------------------------ void rotString(int indy, char *curStr ){ char tmp; int i = 0; if( curStr[indy] == 0) return; else if( curStr[indy+1] == 0) return; else{ i = 1; tmp = curStr[indy]; while( curStr[indy+i] != 0 ){ curStr[indy+i-1] = curStr[indy+i]; i++; } curStr[indy+i-1] = tmp; } } //------------------------------------------------------------ // Generate string permutations //------------------------------------------------------------ void stringPermRec( int indy, int length, char *curString) { // Cycle through the root posabilitys. int i; if( indy < length ){ for( i = indy; i < length; i++){ rotString( indy, curString ); stringPermRec(indy+1, length, curString); } }else{ printf("%s\n", curString); } } //------------------------------------------------------------ // Wrapper for recersive calls //------------------------------------------------------------ void stringPerm( char *curString){ int length = strlen(curString); stringPermRec( 0, length, curString ); } //------------------------------------------------------------ // Main function call //------------------------------------------------------------ #define TESTSTR "1234" int main() { char *testStr = malloc( sizeof(char) * strlen(TESTSTR)); strcpy( testStr, TESTSTR); printf("----- Start -----\n"); stringPerm(testStr ); printf("----- Done -----\n"); fflush(stdout); return(0); }
One reply on “Write a function to print all of the permutations of a string”
This is a non-recursive solution. Also kind of kludgy – I should run through this exercise again before any interviews.