Lab (8) -  CS1440
October 16, 2001

Objective:
To learn how to use the predefined functions (toupper( ), tolower( ), isupper( ), is lower( ), isspace( ), ...) and utilize them to edit a text file.

Before you begin the lab, create the directory lab8.  Then copy the sample.txt file from the instructor's home directory.  Assuming that you are already in the directory lab8, here is the command that you will use to copy the file:

% cp ~rt/public/lab8_files/sample.txt  .
If you need help with the predefined functions, you can use Display 5.8 in the textbook.



Write a C++ code that reads the entire contents of an input file and writes the entire contents of that file to an output file using the following procedure:
(1) The code should capitalize all words that apear after two blank spaces.
    Example:
        INPUT:     The cat was blind.  he was running fast.
        OUTPUT: The cat was blind.  He was running fast.

(2) Write the new file double space (one blank line apart).
(3) Counts the total number of characters in the file, excluding blank spaces, tabs, and new line characters.
(4) Counts the number of capitalizations that has been made.

Example of the input file (file.txt):
An object is a variable that has functions
associated with it.   these functions are
called  member  functions.   a class is a type
whose variables are objects.

Example of the output file (newfile.txt):
An object is a variable that has functions

associated with it.   These functions are

called  member  functions.   A class is a type

whose variables are objects.

What do you see when you run the code:
Please enter the input file: file.txt
Please enter the output file name: newfile.txt

Display:
Total number of characters in this file was:  157
Number of characters that were changed are: 2

Challenging:
Make the file not to write double space those lines which are already double spaced:
Example:
INPUT:
Cat is fast.  it may black.
Dog is slow.

Fox is faster than both.

OUTPUT:
Cat is fast.  It may be black.

Dog is slow.

Fox is faster than both.

An example code:
// *** P81.C
#include<iostream.h>
#include<stdlib.h>
#include<ctype.h>

int main( )
{
    char c;

    cout << "Enter a character \n";
    cin.get(c);

    if( !isspace(c) ) //The character is not a blank space, tab, newline
    {
       if( isalpha(c) ){
          if(isupper(c) )
          {
            cout << "You have input an upper-case character " << c;
            c = tolower(c);
            cout << " I convert it to lower-case " << c<< "\n";
          }
          else
          {
            cout << "You have input a lower-case character " << c;
            c = toupper(c);
            cout << " I convert it to upper-case " << c << "\n";
          }
       }
       else{
            cout << "You entered a number " << c << " \n";
       }
    }
    else
    {
       cout << "You have input a space, tab, or newline \n";
    }
    return 0;
}