-1

Hi again (if anyone saw my earlier post). I already posted a question about this code tonight, but I have almost everything figured out and ran into this little roadblock, so I'm back with another question. For my computer science 162 class, we're given the assignment of creating a simple text editor - but we're only allowed to use cstrings/arrays of characters, so no strings allowed. However, we're allowed to use the cstring class in order to execute certain functions. The text editor must fix small errors, such as: if there is only one space after a period, add a second one; if a simple word such as "the" is spelled wrong (e.g. "teh"), correct it automatically; if the beginning letter of a sentence is not capitalized, capitalize it. Now, I got the function to fix the spaces and the function to change 'teh' to 'the' working just fine, but now I'm having a problem with the function that capitalizes letters at the beginning of a new sentence. Here is my code so far:

#include <iostream>
#include <cstring>
#include <ctype.h>

using namespace std;
void enter_paragraph(char paragraph[]) {
    cout << "Enter a paragraph:";
    cin.get(paragraph, 300, '#');
}
void check_spaces(char paragraph[], char new_para[]) {
    int l = strlen(paragraph);
    int i = 0;
    int n = 0;
    while (i < l) {
        new_para[n] = paragraph[i];
        n++;
        if (paragraph[i] == '.') {
            if (paragraph[i + 1] == ' ') {
                if (paragraph[i + 2] != ' ') {
                    new_para[n] = ' ';
                    n++;
                    new_para[n] = ' ';
                    n++;
                }
            }
        }
        i++;
    }

}
void check_the(char paragraph[], char new_para[]) {
    int l = strlen(new_para);
    int i = 0;
    while (i < l) {
        if (new_para[i] == 't') {
            if (new_para[i + 1] == 'e') {
                if (new_para[i + 2] == 'h') {
                    new_para[i + 1] = new_para[i + 2];
                    new_para[i + 2] = 'e';
                }
            }
        }
        i++;
    }
}
void check_caps(char paragraph[], char new_para[]) {
    int l = strlen(new_para);
    int i = 0;
    while (i < l) {
        if (new_para[i] == '.') {
            if (new_para[i + 1] == ' ') {
                if (new_para[i + 2] == ' ') {
                    if (islower(new_para[i + 3])) {
                        new_para[i + 3] = (toupper(new_para[i + 3]));
                    }
                }
            }
        }
        i++;
    }
}

int main() {
    char paragraph[300];
    /* prompt user to enter a paragraph (no more than 300 characters) */
    enter_paragraph(paragraph);
    cout << "Here is your paragraph: " << endl << paragraph;

    /* check paragraph for two spaces after each paragraph; if there aren't, then change it */
    char new_para[300];
    check_spaces(paragraph, new_para);

    /* check paragraph for misspelling of "the"; if user typed "teh," change it to "the" */
    check_the(paragraph, new_para);

    /* check paragraph for a capitalized first letter after each period; if it is lowercase, change it */
    check_caps(paragraph, new_para);
    cout << "Here is your revised paragraph: " << endl << new_para;

    /*
     etc etc
     output new corrected paragraph (as a new array, preferably)
     */
    return 0;
}

When I run this program, everything goes well except for one thing: if I input a paragraph and put two spaces after the period initially (so that the program doesn't have to add a space), it will capitalize the letter following those spaces; but if I only add one space after a period before beginning the next paragraph (so that the program DOES have to add a space), it doesn't capitalize the first letter. I've tried tweaking so many different things and nothing seems to fix it. Anyone see what I'm doing wrong here?

sbi
  • 219,715
  • 46
  • 258
  • 445
hhoward
  • 159
  • 1
  • 2
  • 12
  • You are overusing spaces in indentation. – Maroun Feb 19 '14 at 07:24
  • are you talking about my question, or is it just a formatting comment? – hhoward Feb 19 '14 at 07:27
  • I think he/she's talking about the code formatting, it's horrible! As for your question and problems, did you already consider using a debugger? – πάντα ῥεῖ Feb 19 '14 at 07:34
  • ok, well i'm a coding newbie and i'm really not here to have my formatting critiqued; this is how i'm supposed to do it for class. I'm just looking for some possible answers to my question. – hhoward Feb 19 '14 at 07:36
  • _'I'm just looking for some possible answers to my question.'_ So lower your expectations, s.o. here is willing to read this lenghty badly formatted code and sport the error for you. – πάντα ῥεῖ Feb 19 '14 at 07:38
  • I ran the code through Eclipse's auto-formatter. Now everyone can concentrate on the content, rather than the package. `:)` You might want to have a [look at this](http://stackoverflow.com/a/1453605/140719), however, @hhoward. – sbi Feb 19 '14 at 07:50

1 Answers1

1

Problem is with your space function.Change it to this and it'll work

 while (i < l) {
        new_para[n] = paragraph[i];
        n++;
        if (paragraph[i] == '.') {
            if (paragraph[i + 1] == ' ') {
                if (paragraph[i + 2] != ' ') {

                    new_para[n] = ' ';
                    n++;
                }
            }
        }
        i++;
    }
G one
  • 2,679
  • 2
  • 14
  • 18