Advertisement
SVXX

Anagram Checker(Working)

Mar 5th, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include <string>
  7. using namespace std;
  8.  
  9.  
  10. void bsort(string &X)
  11. {
  12.   bool bSwapped;
  13.  
  14.    do
  15.    {
  16.       bSwapped = false;
  17.       for (size_t count = 1; count < X.size(); count++)
  18.       {
  19.          if(X[count-1] < X[count])
  20.          {
  21.             swap(X[count-1], X[count]);
  22.             bSwapped = true;
  23.          }
  24.       }
  25.    }
  26.    while(bSwapped);
  27. }
  28.  
  29.  
  30. string delSpaces(string &str) //Remove all spaces
  31. {
  32.    str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
  33.    return str;
  34. }
  35.  
  36.  
  37. int Anagram(string A, string B)
  38. {
  39.     int i;
  40.     A = delSpaces(A);
  41.     B = delSpaces(B);
  42.     cout << "\nThe strings, after removing the whitespaces, are now -:";
  43.     cout << "\nA: " << A << endl;
  44.     cout << "\nB: " << B << endl;
  45.     if(A.size() == B.size()) //Sizes must be equal for anagram
  46.     {
  47.        for(i = 0; i < A.size(); i++)
  48.        {
  49.            A[i] = tolower(A[i]); //Make characters lowercase
  50.            B[i] = tolower(B[i]);
  51.        }
  52.        bsort(A); //Sort both strings
  53.        bsort(B);
  54.        cout << "\nAfter sorting, the strings are -:";
  55.        cout << "\nA: " << A << endl;
  56.        cout << "\nB: " << B << endl;
  57.        if(A.compare(B) == 0)
  58.           return 1;
  59.     }
  60.     return -1;
  61. }
  62.  
  63. int main()
  64. {
  65.     string A, B;
  66.     int x;
  67.     cout << "Enter first sentence : ";
  68.     getline(cin, A);
  69.     cout << "\nEnter second sentence : ";
  70.     getline(cin, B);
  71.     x = Anagram(A, B);
  72.     if(x == -1)
  73.        cout << "\nThus the strings are not anagrams of each other.\n";
  74.     else
  75.        cout << "Thus the strings are anagrams of each other\n";
  76.     system("PAUSE");
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement