Advertisement
Guest User

Anagram

a guest
Aug 17th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <map>
  6. using namespace std;
  7.  
  8. /* Note: words.txt is the original wordlist, words2.txt is the list of scrambled words, and words4.txt is the unscrambled version of words2.txt. */
  9.  
  10. int alpha(char X) //This isn't bugged
  11. {
  12. (int)X;
  13. return X;
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19. ofstream newFile;
  20. newFile.open("C:\\Users\\Owner\\My Documents\\Words4.txt");
  21. int z;
  22. map <string, string> compare;
  23. ifstream myFile("C:\\Users\\Owner\\My Documents\\Words.txt");
  24. if(!myFile){
  25. cout << "Cannot open file.";
  26. cin>>z;
  27. exit (1);
  28. }
  29. char str[30]={};
  30. char strInit[30]={};
  31. char temp;
  32. int check=0;
  33. while(myFile){
  34. myFile.getline(str, 30);
  35. int h=0;
  36. for (; str[h] != 0; h++)//setting the initial version of str
  37. {
  38. strInit[h]=str[h];
  39. }
  40. strInit[h+1]='\0'; //I didn't know if the for loop would include the null char
  41. cout<<strInit;
  42.  
  43. for (int i=0; str[i] != 0; i++) //alphabetizes the current string (apple would become aelpp)
  44. {
  45. for (int j=i; j>0; j--)
  46. {
  47. if (alpha(str[j-1])>alpha(str[j])) //check is for int checking
  48. {
  49. temp=str[j-1]; //temp is for character swapping
  50. str[j-1]=str[j];
  51. str[j]=temp;
  52. }
  53. else
  54. {
  55. check=alpha(str[j]);
  56. }
  57. }
  58. }
  59. compare[str]=strInit;
  60.  
  61. }
  62. myFile.close();
  63. ifstream cinFile("C:\\Users\\Owner\\My Documents\\Words2.txt");
  64. if(!cinFile){
  65. cout << "Cannot open file.";
  66. cin>>z;
  67. exit (1);
  68. }
  69. char cinstr[30];
  70. char cinstrInit[30];
  71. char cintemp;
  72. int cincheck=0;
  73. while(cinFile){
  74. cinFile.getline(cinstr, 30);
  75. int H=0;
  76. for (; cinstr[H] != 0; H++)//setting the initial version of str
  77. {
  78. strInit[H]=str[H];
  79. }
  80. strInit[H+1]='\0'; //I didn't know if the for loop would include the null char
  81.  
  82.  
  83. for (int I=0; cinstr[I] != 0; I++) //alphabetizes the current string (apple would become aelpp)
  84. {
  85. for (int J=I; J>0; J--)
  86. {
  87. if (alpha(cinstr[J-1])>alpha(cinstr[J])) //check is for int checking
  88. {
  89. cintemp=cinstr[J-1]; //temp is for character swapping
  90. cinstr[J-1]=cinstr[J];
  91. cinstr[J]=cintemp;
  92. }
  93. else
  94. {
  95. cincheck=alpha(cinstr[J]);
  96. }
  97. }
  98. }
  99. cout<<compare[cinstr]<<"\n";
  100. newFile<<compare[cinstr]<<",";
  101.  
  102.  
  103. }
  104. cin>>z;
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement