Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstdio>
- #include <windows.h>
- #include <conio.h>
- #include <string>
- using namespace std;
- void bsort(string &X)
- {
- bool bSwapped;
- do
- {
- bSwapped = false;
- for (size_t count = 1; count < X.size(); count++)
- {
- if(X[count-1] < X[count])
- {
- swap(X[count-1], X[count]);
- bSwapped = true;
- }
- }
- }
- while(bSwapped);
- }
- string delSpaces(string &str) //Remove all spaces
- {
- str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
- return str;
- }
- int Anagram(string A, string B)
- {
- int i;
- A = delSpaces(A);
- B = delSpaces(B);
- cout << "\nThe strings, after removing the whitespaces, are now -:";
- cout << "\nA: " << A << endl;
- cout << "\nB: " << B << endl;
- if(A.size() == B.size()) //Sizes must be equal for anagram
- {
- for(i = 0; i < A.size(); i++)
- {
- A[i] = tolower(A[i]); //Make characters lowercase
- B[i] = tolower(B[i]);
- }
- bsort(A); //Sort both strings
- bsort(B);
- cout << "\nAfter sorting, the strings are -:";
- cout << "\nA: " << A << endl;
- cout << "\nB: " << B << endl;
- if(A.compare(B) == 0)
- return 1;
- }
- return -1;
- }
- int main()
- {
- string A, B;
- int x;
- cout << "Enter first sentence : ";
- getline(cin, A);
- cout << "\nEnter second sentence : ";
- getline(cin, B);
- x = Anagram(A, B);
- if(x == -1)
- cout << "\nThus the strings are not anagrams of each other.\n";
- else
- cout << "Thus the strings are anagrams of each other\n";
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement