Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Printing Permutaions of a String
Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.
✍: Guest
void PrintPermu (char *sBegin, char* sRest) { int iLoop; char cTmp; char cFLetter[1]; char *sNewBegin; char *sCur; int iLen; static int iCount; iLen = strlen(sRest); if (iLen == 2) { iCount++; printf("%d: %s%s\n",iCount,sBegin,sRest); iCount++; printf("%d: %s%c%c\n",iCount,sBegin,sRest[1],sRest[0]); return; } else if (iLen == 1) { iCount++; printf("%d: %s%s\n", iCount, sBegin, sRest); return; } else { // swap the first character of sRest with each of // the remaining chars recursively call debug print sCur = (char*)malloc(iLen); sNewBegin = (char*)malloc(iLen); for (iLoop = 0; iLoop < iLen; iLoop ++) { strcpy(sCur, sRest); strcpy(sNewBegin, sBegin); cTmp = sCur[iLoop]; sCur[iLoop] = sCur[0]; sCur[0] = cTmp; sprintf(cFLetter, "%c", sCur[0]); strcat(sNewBegin, cFLetter); debugprint(sNewBegin, sCur+1); } } } void main() { char s[255]; char sIn[255]; printf("\nEnter a string:"); scanf("%s%*c",sIn); memset(s,0,255); PrintPermu(s, sIn); }
2007-02-26, 7405👍, 0💬
Popular Posts:
How To Enter a New Row into a Table Interactively? - Oracle DBA FAQ - Introduction to Oracle SQL Dev...
What Information Is Needed to Connect SQL*Plus an Oracle Server? - Oracle DBA FAQ - Introduction to ...
interview.FYIcenter.com offers a collections of interview questions and answers for software and Web...
What are the two fundamental objects in ADO.NET ? Datareader and Dataset are the two fundamental obj...
How To Remove the Top White Space of Your Web Page? - CSS Tutorials - Introduction To CSS Basics The...