I have a program that deals with anagrams and for the most part the program works but on a few of the words that should be it says that they aren't and one says it is and it's not. can someone help?
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string anagram(const string& firstWord, const string& secondWord);
int main()
{
string firstWord; // First word in the file
string secondWord; // Second word in the file
//opens data file
ifstream in("filename.txt");
// tell the computer to print out an error message if not in file
if(!in.is_open())
{
cout << "Can't open input file\n";
}
// tell the program that while in the file continue to run
while( in >> firstWord >> secondWord)
{
cout << " " << firstWord << " " << secondWord << endl;
anagram(firstWord, secondWord);
cout << endl << " Press Enter to continue " << endl;
cin.get();
}
in.clear();
in.close();
// print a closing message and exit program
cout << endl << "-=- Execution Terminated -=-" << endl << endl;
cout << "==================================================" << endl
<< endl;
return 0;
}
//This function tests two inputted words to see if they are anagrams.
string anagram(const string& firstWord, const string& secondWord)
{
//This gets the length of the first string.
int lengthFirstWord = firstWord.length();
//This gets the length of the second string.
int lengthSecondWord = secondWord.length();
//This sets the test to true to start the testing loops.
bool testAnagram = true;
//This is copying the second word for testing.
string temp(secondWord);
//These output the length of the strings.
cout << " The length of the first word is: " << lengthFirstWord << endl;
cout << " The length of the second word is: " << lengthSecondWord << endl;
//The if statement test to see if the length of the words are equal.
//If they are not then they are not anagrams.
if (lengthFirstWord == lengthSecondWord)
{
int index1 = 0;
//This starts the testing by getting the first character.
while( index1 < lengthFirstWord && testAnagram != false)
{
//Takes one character and puts it in 1 character string.
string testingChar = firstWord.substr(index1,1);
//This sets the the tester to false. This will end the first loop if the
//character is not found.
testAnagram = false;
//This tests a character from the first string against the characters of
//the second string.
for (int index2 = 0;index2 < lengthSecondWord; index2++)
{
//Takes one character and puts it in 1 character string.
string testedChar = temp.substr (index2,1);
//Test if the characters match.
if (testingChar == testedChar)
{
//Changes the test condition to true to continue test the rest of the word
//string.
testAnagram = true;
//Removes the instance of the characters matching.
for(int index3=index2;index3<lengthSecondWord;index3++)
{
//This is just replacing the character that was matched.
temp[index3] = temp[index3+1];
}
//Reduces the size of the second word.
//lengthSecondWord--;
//Since the character was found in the second word. Goes to the next character.
continue;
}
}
index1++;
}
}
else
{
//Since the words are not the same length they cannot be anagrams.
cout << " The length of the words are not equal. They are not an anagram." <<
endl << endl;
return firstWord;
}
//Test to see if the test condition is false. Indicating that a character
//was not found.
if (testAnagram == false)
{
//If the test condition was false then the wards are not anagrams.
cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
"! " << endl << endl;
}
else
{
//If the test condition was true then the words are anagrams.
cout << " " << firstWord << " and " << secondWord << " are anagrams! "
<< endl;
}
return firstWord;
}