Write a C program 1 Study the function processtext in file t

Write a C++ program

1. Study the function process_text() in file \"text_processing.cpp\". What do the string member functions find(), substr(), c_str() do?

//text_processing.cpp

#include \"util.h\"
#include \"text_processing.h\"
#include <string.h>

using namespace std;

int process_text( fstream &ifs, fstream &ofs, char target[] )
{
string replace_str( \"XXX\" );   //hard-coded string for replacement
int tlen = strlen( target );   //string length of target
int max_len = 200;       //maximum length of a line
char s[max_len+1];
clear_screen();           //clear the screen
while ( !ifs.eof() ) {
    int i2, i1 = 0, len=0;
    ifs.getline( s, max_len );       //get one line from file
    string str( s );           //construct a string object
    i2 = str.find ( target, i1 );   //find target
    len = i2 - i1;
    if ( i2 > -1 && len > 0 ){       //if target found
      print_text( str.substr( i1, i2 ).c_str() ); //print up to target
      ofs << str.substr( i1, i2 );
    }
    while ( i2 > -1 ) {
      print_text_inverse ( target );   //highlight target
      ofs << replace_str;
      i1 = i2 + tlen;           //new search position
      i2 = str.find ( target, i1 );   //find next target
      len = i2 - i1;
      if ( i2 > -1 && len > 0 ){           //found target
          print_text( str.substr( i1, len ).c_str() ); //print up to target
   ofs << str.substr( i1, len );
      }
    }
    len = str.length() - i1;
    if ( len > 0 ) {       //print the remainder
        print_text( str.substr( i1, len ).c_str() );
   ofs << str.substr( i1, len );
    }
    ofs << endl;
    getnstr( s, 1 );       //prompt for <Enter>
} //while ( !ifs.eof() )
restore_screen();       //restore the old screen
return 1;
}

//text_processing.h
#ifndef TEXT_PROCESSING_H
#define TEXT_PROCESSING_H

#include <string>
#include <fstream>

using namespace std;
int process_text( fstream &ifs, fstream &ofs, char target[] );

#endif

//util.h
#ifndef   UTIL_H
#define   UTIL_H


#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <curses.h>

using namespace std;

void usage ( char program_name[] );
void clear_screen();
void print_text( const char *s );
void print_text_inverse( const char *s );
void get_input_text( char *s, int max_len );
void restore_screen();
#endif

//util.cpp

#include \"util.h\"

void usage ( char program_name[] )
{
cout << \"Usage: \" << program_name << \" infile outfile search_string\" << endl;
return;
}

/*
The following are some curses functions. You can find
the details by the command \"man ncurses\". But its use
can be transparent to you.

*/
void clear_screen()
{
initscr();
scrollok ( stdscr, true );   //allow window to scroll
}

void print_text ( const char *s )
{
printw(\"%s\", s );
refresh();
}

void print_text_inverse( const char *s )
{
attron( A_REVERSE );
printw(\"%s\", s );
attroff( A_REVERSE );
refresh();
}

void get_input_text( char *s, int max_len )
{
getnstr( s, max_len );
}

void restore_screen()
{
endwin();
}

//str_main.cpp

#include \"util.h\"
#include \"text_processing.h\"
#include <string.h>

using namespace std;

/*
Note that grace_open() must be defined in same file
as main(), otherwise the destructor of fstream will
automatically close the opened file as it finds that
its out of scope upon exiting the function.
*/
int grace_open( char *s, fstream &fs, char *mode )
{
if ( mode == \"in\" )
    fs.open ( s, ios::in );
else if ( mode == \"out\" )
    fs.open ( s, ios::out );

if ( fs.fail() ) {
    cout << \"error opening file \" << s << endl;
    return -1;
} else
    return 1;
} //grace_open


int main( int argc, char *argv[] )
{
const int max_len = 200;
char s[max_len+1];
char   target[100];
if ( argc < 4 ) {
    usage( argv[0] );
    return 1;
}
char *pin = argv[1];       //pointing to input filename
char *pout = argv[2];       //pointing to output filename
strcpy ( target, argv[3] );   //target for search
fstream ifs, ofs;       //input output filestream

if ( grace_open ( pin, ifs, \"in\" ) < 0 )
    return 1;           //fail
if ( grace_open ( pout, ofs, \"out\" ) < 0 )
    return 1;           //fail
process_text( ifs, ofs, target );
return 0;
}

2. Modify ( or write your own ) the program so that it does the following.

    1. It displays a detailed help menu when one executes
      find_pat --help

    2. It asks for an additional input argument, \"replacing string\" in addition to the three arguments described above. Namely, the four input arguments are: input filename, output filename, target string, replacing string. Instead of replacing the target strings by \"XXX\", replace them by the replacing string in the output file. For example

       find_pat text_processing.cpp out.txt in abc

       will replace all \"in\" by \"abc\" in \"out.txt\".

    3. Highlight the \"replacing string\" on the screen instead of the \"target string\". ( Do not need to display the \"target string\" any more. )

Solution

1. int find(const string & s, int pos = 0) it searches the invoking string for s starting at index pos and returns the index where s is first located.

substr() return a substring from the invoking string.

c_str() it converts string to c styled string. it returns a const char* that points to a null-terminated c styled basic string.

2.

#include <fstream>
#include <string.h>

#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <curses.h>

#include <string>
#include <fstream>

using namespace std;

void usage ( char program_name[] )
{
cout << \"Usage: \" << program_name << \" infile outfile search_string replaced_string\" << endl;
return;
}

void clear_screen()
{
initscr();
scrollok ( stdscr, true ); //allow window to scroll
}
void print_text ( const char *s )
{
printw(\"%s\", s );
refresh();
}
void print_text_inverse( const char *s )
{
attron( A_REVERSE );
printw(\"%s\", s );
attroff( A_REVERSE );
refresh();
}
void get_input_text( char *s, int max_len )
{
getnstr( s, max_len );
}
void restore_screen()
{
endwin();
}

int process_text( fstream &ifs, fstream &ofs, char target[], char rep_str[] )
{
string replace_str( rep_str ); //hard-coded string for replacement
int tlen = strlen( target ); //string length of target
int max_len = 200; //maximum length of a line
char s[max_len+1];
clear_screen(); //clear the screen
while ( !ifs.eof() ) {
int i2, i1 = 0, len=0;
ifs.getline( s, max_len ); //get one line from file
string str( s ); //construct a string object
i2 = str.find ( target, i1 ); //find target
len = i2 - i1;
if ( i2 > -1 && len > 0 ){ //if target found
print_text( str.substr( i1, i2 ).c_str() ); //print up to target
ofs << str.substr( i1, i2 );
}
while ( i2 > -1 ) {
print_text_inverse ( rep_str ); //highlight target
ofs << replace_str;
i1 = i2 + tlen; //new search position
i2 = str.find ( target, i1 ); //find next target
len = i2 - i1;
if ( i2 > -1 && len > 0 ){ //found target
print_text( str.substr( i1, len ).c_str() ); //print up to target
ofs << str.substr( i1, len );
}
}
len = str.length() - i1;
if ( len > 0 ) { //print the remainder
print_text( str.substr( i1, len ).c_str() );
ofs << str.substr( i1, len );
}
ofs << endl;
getnstr( s, 1 ); //prompt for <Enter>
} //while ( !ifs.eof() )
restore_screen(); //restore the old screen
return 1;
}


int grace_open( char *s, fstream &fs, char *mode )
{
if ( mode == \"in\" )
fs.open ( s, ios::in );
else if ( mode == \"out\" )
fs.open ( s, ios::out );
if ( fs.fail() ) {
cout << \"error opening file \" << s << endl;
return -1;
} else
return 1;
} //grace_open

int main( int argc, char *argv[] )
{
const int max_len = 2000;
char s[max_len+1];
char target[100];
char replace[100];
if ( argc < 5 ) {
usage( argv[0] );
return 1;
}
char *pin = argv[1]; //pointing to input filename
char *pout = argv[2]; //pointing to output filename
strcpy ( target, argv[3] ); //target for search
strcpy ( replace, argv[4] ); //target for search
fstream ifs, ofs; //input output filestream
if ( grace_open ( pin, ifs, \"in\" ) < 0 )
return 1; //fail
if ( grace_open ( pout, ofs, \"out\" ) < 0 )
return 1; //fail
process_text( ifs, ofs, target, replace );
return 0;
}

Write a C++ program 1. Study the function process_text() in file \
Write a C++ program 1. Study the function process_text() in file \
Write a C++ program 1. Study the function process_text() in file \
Write a C++ program 1. Study the function process_text() in file \
Write a C++ program 1. Study the function process_text() in file \
Write a C++ program 1. Study the function process_text() in file \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site