I am having a problem when it comes to the are_anagrams() fuction because when I call make_upcase() inside it, it does not captialize all of the contents of the string. I think it may be a problem with passing by value vs by reference, but still can't get the right results.
Code:
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
string make_upcase(string &s) ;
int count_words(string &s) ;
bool are_anagrams(string &s, string &s2) ;
string sort (string &s) ;
string no_spaces(string &s) ;
int main()
{
string input_filename , output_filename ;
ifstream INFILE ;
ofstream OUTFILE ;
cout <<"Enter the input filename: " ;
cin >> input_filename ;
INFILE.open(input_filename.c_str()) ;
//Checks if file opened successfully. If not, the program exits.
if(INFILE.fail())
{
cout <<"File " << input_filename << " not opened!" << "Exiting Program!" ;
system("Pause") ;
exit(1) ;
}
//Checks if the input file is empty. If empty, the program exits.
if(!INFILE)
{
cout <<"Error: Input file is empty!" << " " << "Exiting Program." ;
system("Pause") ;
exit(1) ;
}
cout <<"Enter the output filename: " ;
cin >> output_filename ;
OUTFILE.open(output_filename.c_str()) ;
string line, line2, cur_line, next_line ;
while (INFILE)
{
while( getline(INFILE, line) )
{
if(!isdigit(line[0])) //Ignores lines that start with integers
{
break ;
}
}
cur_line = line ;
while( getline(INFILE, line) )
{
if(!isdigit(line[0])) //Ignores lines that start with integers
{
break ;
}
}
next_line = line ;
if(!cur_line.empty() && !next_line.empty())
{
OUTFILE << cur_line << endl << make_upcase(cur_line) << endl
<< count_words(cur_line) << " characters in the string."
<< "\n" << "\n" << next_line << endl << make_upcase(next_line)
<< endl << count_words(next_line) << " characters in the string."
<< endl << "\n" ;
if(are_anagrams(cur_line, next_line))
{
OUTFILE << "They are anagrams" << endl << "\n" ;
}
else
{
OUTFILE << "They are not anagrams" << endl << "\n" ;
}
}
} // End of while(INFILE) loop
system("Pause");
return 0;
}// End main
string make_upcase(string &s)
//----------------------------------------------------------------------------
//Creates a new string that is an upper case version of the original string.
//Pre-Condition: Original string is lowercase.
//Post-Condition: New String is all Caps.
//----------------------------------------------------------------------------
{
string uppercase_string = s ; //copy of string
if(! s.empty()) //Does the following if the string is not empty
{
for(int i = 0; i < uppercase_string.length(); i++)
{
uppercase_string[i] = toupper(s[i]) ; //coverts single character to uppercase
//uppercase_string.insert(i,letter) ;
}
}
return uppercase_string ;
}
int count_words(string &s) //determines how many words are in the string.
{
return s.length() ;
}
bool are_anagrams(string &s, string &s2)
//----------------------------------------------------------------------------
//Determines whether the strings are anagrams of one another.
//Pre-Condition: Two strings are passed to the function, original string and
// uppercase string.
//Post-Condition: If the two strings are anagrams of eachother, the function
// returns true, else it returns false.
//----------------------------------------------------------------------------
{
if(!s.empty())
{
make_upcase(s) ; //Capitalize all characters in the strings since
make_upcase(s2) ; //case is ignored with anagrams.
cout << s << endl << s2 << endl ;
no_spaces(s) ; //Remove all spaces from the strings
no_spaces(s2) ;
sort(s) ; //Sort the strings passed to are_anagrams()
sort(s2);
if(s.compare(s2) == 0)
{
return true ;
}
else
{
return false ;
}
}
} //end of function definition
string sort (string &s)
//-------------------------------------------
//Sorts a string
//Pre-condition: Receives an unsorted string
//Post-condition: Returns a sorted string
//-------------------------------------------
{
char tmp ;
for(int i = 0; i < s.size(); i++)
{
for(int j = 0; j < s.size(); j++)
{
if(s[i] > s[j])
{
tmp = s[i] ;
s[i] = s[j] ;
s[j] = tmp ;
}
} //end of int j for loop
} //end of int i for loop
return s ;
}//end of function definition
string no_spaces(string &s)
{
int pos ;
string pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.?:',\n";
while(s.find_first_not_of(pattern) != string::npos)
{
pos = s.find_first_not_of(pattern) ;
s.erase(pos,1) ;
}
return s ;
}