Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void RecursivePermute(char str[], int k, FILE *fprt);
  6. void ExchangeCharacters(char str[], int i, int j);
  7.  
  8.  
  9. int main()
  10. {
  11. char str[10] = "";
  12. char word[10] = "" ;
  13. FILE *file = fopen("AssignmentThreeInput.txt", "r");
  14. FILE *fptr = fopen("AssignmentThreeOutput.txt", "w");
  15.  
  16.  
  17. if(file == NULL || fptr == NULL)
  18. printf("file not found");
  19.  
  20.  
  21. while(!feof(file))
  22. {
  23. fgets(str, 10 , file);
  24. strcpy(word, str);
  25. printf("working on word %s", word);
  26.  
  27. printf("%d", h);
  28. system("pause");
  29. RecursivePermute(word, 0, fptr);
  30.  
  31. }
  32. fclose(file);
  33. fclose(fptr);
  34. return;
  35. }
  36.  
  37.  
  38.  
  39. void RecursivePermute(char str[], int k, FILE *fptr)
  40. {
  41.  
  42. int j;
  43.  
  44. // Base-case: All fixed, so print str.
  45. if (k == strlen(str))
  46. {
  47. fprintf(fptr, "%s\n", str);
  48. }
  49. else
  50. {
  51. // Try each letter in spot j.
  52. for (j = k; j < strlen(str); j++)
  53. {
  54.  
  55.  
  56. ExchangeCharacters(str, k, j);
  57.  
  58.  
  59. RecursivePermute(str, k + 1, fptr);
  60.  
  61.  
  62. ExchangeCharacters(str, j, k);
  63.  
  64. }
  65. }
  66.  
  67. }
  68.  
  69.  
  70. void ExchangeCharacters(char str[], int i, int j)
  71. {
  72.  
  73. char temp = str[i];
  74. str[i] = str[j];
  75. str[j] = temp;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement