Capitalizing

Hi, I wrote this code first then was asked to modify it so that the program read the contents of the first file and change all the letters to lowercase except the first letter of each sentence, which should be made uppercase. The revised contents should be stored in the second file. How can i form a loop that can do this successfully?

This is the original code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main()
{
    // Variables needed to read and write files.
    const int LENGTH = 101;
    ifstream inFile;                        // For input file
    ofstream outFile("myOutput.txt");       // For output file
    char myInput[LENGTH], ch, ch2;
    string myOutput;

    cout << "Lori Bailey" << endl << endl;

    // Open input file
    cout << "Enter two file names: ";
    cin >> myInput >> myOutput;
    inFile.open(myInput);
    if (!inFile)
    {
        cout << "Cannot open " << myInput << endl;
        return 0;
    }

    // Read characters from  input file and write uppercased
    // versions of the character to the output file.
    //myInput[0] = toupper(myInput[0]);
    while (inFile.get(ch))
        {
            ch2 = toupper(ch);
            outFile.put(ch2);
        }

    // Close files.
    inFile.close();
    outFile.close();
    cout << "File conversion done.\n";
You should add a variable to keep track of whether the code is in the middle of a sentence or not. Whenever you encounter a period, exclamation point, or question mark, set the variable to false (you are no longer in the sentence). If you encounter a letter while the variable is false (the first letter of a sentence), skip the ch2 = tolower(ch2) and set the variable to true. Otherwise, run ch2 = tolower(ch).
ok I modified it to this:

1
2
3
4
5
6
7
8
9
10
    while(inFile.get(ch))
    {
        if('A' <= ch <= 'z'){
            new_sentence = false;
            outFile.put(tolower(ch));}
        else if(ch == '.' || '?' || '!'){
            new_sentence = true;
            ch2 = toupper(ch);
            outFile.put(toupper(ch2));}
    }


All this is doing is making everything lowercase. It isn't changing the first letter of each sentence to uppercase. Any idea as to why?
This is kind of like a question for your answer.
Is it possible to make the first letter it's own variable/string?
how does this compile with statements like this:
1
2
if('A' <= ch <= 'z')
else if(ch == '.' || '?' || '!')


also, you might find the isupper(), islower() and ispunct() functions useful for this project

edit: i guess it would probably compile, but it's not going to give you the results you want.

you need to split all that up into separate comparisons

1
2
if('A' <= ch && ch <= 'z')
else if(ch == '.' || ch == '?' || ch == '!')
Last edited on
it gave no build errors, it simply just did not uppercase the first letters of each sentence
here's what i would do

1
2
3
4
5
6
7
8
9
10
have a while loop that loops until the end of the file
   use get() to capture one character and read it into a single temp char
   use isspace() to loop until you receive a non-whitespace character
      receive non-whitespace character and stop looping
      use toupper() to capitalize it
   loop until you receive a character that ends a sentence
      for each of the characters, use tolower() to lower case them
      receive a period, question mark, etc., stop looping
   write every character you receive to the output file (after modifying it if applicable)
end of outer loop
Last edited on
Topic archived. No new replies allowed.