Site hosted by Angelfire.com: Build your free website today!

Part1

         outline of assignment

 

#include <iostream.h> //cin and cout

#include <fstream.h> // file defining istream object

#include <Ctype.h> // isalpha function

 

int main () {

ifstream inFile; // declare variable of type ifstream

ofstream outFile; // declare variable of type ofstream

char ch;

int i = 0, j=0;

 

 

/* open existing source file md.txt */

inFile.open("md.txt", ios::nocreate);

 

if ( inFile.fail() ) { // file open failed

cout << "ERROR in opening infile dm.txt." << endl;

return 0;

}

 

/* create new 'out.txt', open it for writing */

outFile.open("out.txt", ios::out);

 

if ( outFile.fail() ) { // file open failed

cout << "ERROR in opening outfile." << endl;

return 0;

}

 

/* file successfully opened */

while ( !inFile.eof() ) { // still more data to read

 

ch = (char) inFile.get(); // read one char in

i++;

if (i > 90000) {

if (isalpha(ch)) {

outFile << ch;

j++;

}

}

} // end while loop

 

outFile << '\0';

cout << "Trim 90000 out and write the rest " << j << " Characters out to \"out.txt\" Successfully, Bye!" << endl;

 

inFile.close(); // close inFile

outFile.close(); // close outFile

return 0;

 

} // end fn. Main

 

 

Part2

 

#include <iostream.h>

#include <fstream.h>

#include <string.h>

#include <Ctype.h>

 

// the function declarations

char *read_text(ifstream);

bool find_target(char *, char *);

 

int main() {

 

char *target; // a pointer target pointing to word searching for

target = new char[100]; // a fixed array for the input searching word

 

char *text; // a pointer text pointing to the text searching from

bool found;

 

ifstream inFile;

 

inFile.open("out.txt", ios::nocreate);

 

if ( inFile.fail() ) { // file open failed

cout << "ERROR in opening infile out.txt." << endl;

return 0;

}

 

text = read_text(inFile);

 

while ( true )

{

cout << "Enter a search String (null to terminate): ";

// read the input

cin >> target;

 

// stop the program if the user input "null"

if ( strcmp(target, "null") == 0 )

break;

 

// print the length of the searching string

cout << "Length is: " << strlen(target) << endl;

 

// invoke the position() function to compute the start position

// and the equudistant letter sequence

found = find_target(target, text);

 

if (!found)

cout << "Sorry, not found!" << endl;

}

 

delete [] text;

delete [] target;

 

cout << "Thank you for using the program, Bye!" << endl;

 

return 0;

}

 

char* read_text(ifstream in_stream)

{

// get the size of the input file

in_stream.seekg(0,ios::end);

int size = in_stream.tellg();

in_stream.seekg(0, ios::beg);

// allocate a dynamic array for pointer text

char *text = new char[size];

 

in_stream.read(text, size);

 

in_stream.close();

 

// return the convert result

return text;

}

 

 

bool find_target(char *target, char *text)

{

char *pt, *pt2, *pt1, *pt3;

//pt pointing to the beginning of text and so on,

//pt1 pointing to the beginning of target and so on,

//pt2 pointing to the position of tolower(*pt) == tolower(*pt1)

 

int start, skip, total;

 

total = strlen(text);

pt = text;

 

for (;*pt != 0; ++pt) { // loop through to find first matching char

if (tolower(*pt) == tolower(*target)) // first letter matched

{

bool found = false;

start = pt - text + 1;

 

for (pt2 = pt+1; *pt2!= 0; pt2++) {

// loop through to find second matching char

pt1 = target + 1;

 

if (pt2-text >= total || *pt1 == 0) {

if (strlen(target) == 1) {

//searching word containing one char

cout << "Match starts at: " << start << ", skip is 0" << endl;

return true;

}

break; //no more text to search

}

 

if (tolower(*pt2) == tolower(*pt1)) {

// second letter matched

 

found = true;

skip = pt2-pt; //determine the length for skipping

pt1++;

 

pt3 = pt2;

for (; *pt1 != 0; ++pt1) {

//check whether rest chars matched

pt3 += skip;

 

if (pt3-text >= total) {

found = false;

break;

}

 

if (tolower(*pt3) != tolower(*pt1)) {

found = false;

break;

}

} //rest matched

if (found) {

// if found one then print out on the screen

cout << "Match starts at: " << start << ", skip is " << skip << endl;

return true;

}

} //second matched

}//check second matching loop

 

}//first matched

} //check first matching loop

return false;

}