简体   繁体   中英

Using Strncpy to create a simple text editor in C++

So, 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 (eg "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 working just fine, but the function to check for "teh" and change it to "the" is tripping me up. Here's my program so far:

enter_paragraph(char paragraph[])
{
    cout <<"Enter a paragraph:";
    cin.getlin(paragraph,300,"#");
    cout <<"Here is your paragraph: " <<endl<<paragraph;
}

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++;
    }

}

check_the()
{
    int l = strlen(new_para);
    int i = 0;
    char
    while(i<l)
    {
        if(new_para[i] == 't')
        {
            if(new_para[i+1] == 'e')
            {
                if(new_para[i+2] == 'h')
                {
                    strncpy(i+
} 

check_caps()
{
}

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;

/* user enters paragraph; program stores it */

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

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

/* check paragraph for a capitalized first letter after each period; if it is lowercase, change it */
    check_caps();

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

I know that there are some errors in the main function, but I'm not worrying about those right now. I just need help with the check_the function. How can I utilize strncpy to accomplish what I need to do? If there's a better way that I'm missing, what is it? Thanks so much.

Why do you want to use "strncpy"? You can do a simple replacement by making newpara[i+1] = newpara[i+2]; Newpara[i+2]='e';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM