www.6park.comusing namespace std; www.6park.com
int main()
{
string line = "";
string first = "";
string current = "";
string next = ""; www.6park.com
ifstream inputFile("input.txt"); www.6park.com
// Open and Read File www.6park.com
if ( inputFile.is_open() )
{
while (inputFile.good() )
{
getline (inputFile,line); www.6park.com
if ( line.length() > 0 )
{
cout << endl << "The current line from the file is => " << line << endl; www.6park.com
// For each line, we print out BEFORE or AFTER for each world www.6park.com
first = ""; www.6park.com
for ( int i=0; i < line.length(); i=i+1 )
{
// Building the first word www.6park.com
if ( first == "" && (int)toupper(line[i]) != 32 )
{
current = current + line[i];
}
else
if ( next == "" && (int)toupper(line[i]) == 32 )
{
// Print out the first word
first = line[0];
cout << current << " COMES FIRST";
} www.6park.com
// Building the second word
if ( first != "" && (int)toupper(line[i]) != 32 )
{
next = next + line[i];
} www.6park.com
if ( next != "" && (int)toupper(line[i]) == 32 )
{
if ( (int)toupper(next[0]) > (int)toupper(current[0]) && next != "end" )
{
cout << endl << next << " AFTER " << current;
} www.6park.com
if ( (int)toupper(next[0]) < (int)toupper(current[0]) && next != "end" )
{
cout << endl << next << " BEFORE " << current;
} www.6park.com
if ( (int)toupper(next[0]) == (int)toupper(current[0]) && next != "end" )
{
cout << endl << next << " SAME AS " << current;
} www.6park.com
current = next;
next = "";
} www.6park.com
} www.6park.com
if ( current == "end" || next == "end" )
{
first = "";
current = "";
next = ""; www.6park.com
cout << endl << endl;
} www.6park.com
}
} www.6park.com
cout << endl;
inputFile.close(); www.6park.com
}
else
{
cout << "Unable to open file";
} www.6park.com
return 0; www.6park.com
} www.6park.com
=== www.6park.com
> cp cpp01.cpp cpp01.cc
> g++ cpp01.cc
> ./a.out www.6park.com
The current line from the file is => Alpha Tango Bravo Epsilon Epsilon Delta end
Alpha COMES FIRST
Tango AFTER Alpha
Bravo BEFORE Tango
Epsilon AFTER Bravo
Epsilon SAME AS Epsilon
Delta BEFORE Epsilon www.6park.com
The current line from the file is => Alpha CTango Fravo Epsilon Epsilon Delta end
Alpha COMES FIRST
CTango AFTER Alpha
Fravo AFTER CTango
Epsilon BEFORE Fravo
Epsilon SAME AS Epsilon
Delta BEFORE Epsilon www.6park.com
www.6park.com