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?