I did something simmilar way back, hope it helps:
INPUT FILES - space counts as character
input.txt - Anagrams
3
ant
4
t an
input1.txt - Anagrams
3
ant
3
tan
input2.txt - NOT Anagrams
3
ast
3
tan
input3.txt - Empty File - Recive Error Message
input4.txt - Anagrams
14
do your own
13
word on you
input5.txt - NOT Anagrams
14
do your own
13
word on yyu
input6.txt - NOT Anagrams
14
do your own
input7.txt - NOT Anagrams
40
The quick brown fox jumps over a lazy do
41
The quick brown fox jumps over a lay dog.
input8.txt - NOT Anagrams
4
clasp
5
scalp
input9.txt - NOT Anagrams
3
3
tan
Code:
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
//functions prototypes
void make_upcase (string input_string[] , int size , int count) ; // converts letters to upper case
int count_words ( int string_number , string input_string[]) ; // counts words on a line
bool are_anagrams (string input_string[] , int string_lenght[] , int position);
void swap (char &first_char , char &next_char);
////////////////////////////////////////////////////////////////////////////////
int main()
{
ifstream input_data;
input_data.open ("input.txt");
if (! input_data) // File was no found
{
cout << "File \"input.txt\" not found" << endl << endl;
system ("pause");
exit(1);
}
else // File was found
{
cout << "File \"input.txt\" found" << endl;
int temp_num; // temporary int for storing number of chars
int size = temp_num; // size of input string
int count = 0; // counter for loop
// read data from file and move it to the input_string dynamic array
string * input_string = new string [size] ; // dynamic allocated array
cout << "This file looks like:" << endl;
while (!input_data.eof())
{
getline(input_data, input_string [count] ) ;
cout << input_string[count] << endl; // show original input file
count ++;
} // end while loop
input_data.close(); // close ifstream
if (count == 1) //File is empty
{
cout << "File \"input.txt\" is empty" << endl << endl;
system ("pause");
exit(1);
} // end if
// splitt array --- take numbers and put it into new array
int * string_lenght = new int[size];
int convert;
for ( int i = 0 , j = 0; i < count ; i+=2)
{
convert = atoi(input_string[i].c_str()); // c_str() converts
// string into char[]
// atoi converts
// char[] into int
string_lenght[j] = convert; // moves numbers form input_num array
// to string_lenght array
j++;
convert++;
} // end for loop
cout << endl << endl;
// function calls
// function to convert letters to upper case
make_upcase (input_string , size , count) ;
// precondition: input_string ia s array of strings from input.txt file,
// where each element is a single word. Size is the size
// of array. Count is the number of lines in file
// postcondition: letters are converted to upper case
// ****************************************************************** //
// function to count number of words in each sentence function is called
// only for odd elements of input_string since the even represent count
for ( int string_number = 1 ; string_number < count ; string_number+=2)
{
int num = count_words ( string_number , input_string) ;
// precondition: string_number is number of string (only odd)
// input_string is a array of strings from file
// postcondition: returns number of words in each sentence
cout << "String " << input_string[string_number]<< endl <<
" has: " << num << " word(s), " << input_string[string_number-1]
<< " characters"<< endl;
} // end for loop
// ****************************************************************** //
// function to check is 2 sentences are anagrams of each other
int position = 0;
bool anagrams = are_anagrams ( input_string , string_lenght , position);
// precondition: string_number is number of string (only odd)
// string_lenght is the number of characters in each line
// after they were converted into intigers from a string
// position coresponds to the index of an array
// postcondition: returns true if two strings are anagrams and false if
// they are not anagrams
if (anagrams != false)
{
cout << endl << "They are anagrams." << endl << endl;
system ("pause");
exit(1);
}
else
{
cout << endl << "They are not anagrams." << endl << endl;
system ("pause");
exit(1);
}
} // end if/else
system ("pause");
return 0;
}
// FUNCTIONS
// converts all letters into upper case
void make_upcase (string input_string[] , int size , int count)
{
string * words = new string[size]; // new array to hold uppercase words
for ( int i = 1 , j = 0 ; i < count ; i+=2)
{
strupr((char *) input_string[i].c_str()); // Function converts to upper case
words[j] = input_string[i]; // move words from input_string array to words array
j++;
} // end for loop
} // end function
////////////////////////////////////////////////////////////////////////////////
// counts words on a line
int count_words (int string_number , string input_string[])
{
int word_count = 0;
string temp_string = input_string[string_number];
string buffer; // Create a buffer string
stringstream string_stream (temp_string); // Insert temp_string into a stream
while (string_stream >> buffer)
word_count++;
return word_count;
} // end function
////////////////////////////////////////////////////////////////////////////////
// checks if 2 sentences are anagrams
bool are_anagrams (string input_string[] , int string_lenght[] , int position )
{
// each element will hold one char from input_string
vector <char> text;
vector <char> text1;
// convert each string (element in array) into array of chars
const char *char_text = input_string[1].c_str();
const char *char_text1 = input_string[3].c_str();
// puts each char as seperate element into vector
for (int i = 0 ;i < string_lenght[0] ; i++)
text.push_back (char_text[i]);
// puts each char as seperate element into vector
for (int i = 0 ;i < string_lenght[1] ; i++)
text1.push_back (char_text1[i]);
// Sort vector holding first set of chars alphabeticly
for (int i = 0; i < string_lenght[position]; i++)
{
for (int j = i; j < string_lenght[position]; j++)
if (text [i] > text [j])
swap (text[i], text[j]);
} // end for loop
// Sort vector holding next set of chars alphabeticly
for (int i = 0; i < string_lenght[1]; i++)
{
for (int j = i; j < string_lenght[1]; j++)
if (text1 [i] > text1 [j])
swap (text1[i], text1[j]);
} // end for loop
int x = text.size();
int x1 = text1.size();
// removes empty elements from vector
for (int i = 0; i < text.size() ; i++)
{
if (text[i] == ' ')
{
for (int erase = i; erase < text.size()-1; erase++)
text[erase ] = text[ erase+1 ] ;
text.pop_back() ;
} // end if
} // end for loop
// removes empty elements from vector
for (int i = 0; i < text1.size() ; i++)
{
if (text1[i] == ' ')
{
for (int erase = i; erase < text1.size()-1; erase++)
text1[erase ] = text1[ erase+1 ] ;
text1.pop_back() ;
} // end if
} // end for loop
// compare each element of the 2 vectors, if they are equal return true
// since the strings are anagrams of each other, else return false
if (text == text1)
return true;
else
return false;
} // end function
////////////////////////////////////////////////////////////////////////////////
// swaps characters for sorting chars alphabeticly
void swap (char &first_char , char &next_char)
{
char temp = first_char;
first_char = next_char;
next_char = temp;
} // end function