繁体   English   中英

使用Strncpy在C ++中创建一个简单的文本编辑器

[英]Using Strncpy to create a simple text editor in C++

因此,对于我的计算机科学162类,我们给出了创建一个简单文本编辑器的任务 - 但我们只允许使用cstrings /字符数组,因此不允许使用任何字符串。 但是,我们允许使用cstring类来执行某些功能。 文本编辑器必须修复小错误,例如:如果一段时间后只有一个空格,则添加第二个空格; 如果一个简单的单词如“the”拼写错误(例如“teh”),则自动纠正; 如果句子的开头字母没有大写,则将其大写。 现在,我得到了修复空间正常工作的功能,但检查“teh”并将其更改为“the”的功能正在绊倒我。 到目前为止这是我的程序:

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等输出新的更正段落(最好是新数组)* /}

我知道主要功能有一些错误,但我现在并不担心这些错误。 我只需要check_the函数的帮助。 我如何利用strncpy来完成我需要做的事情? 如果有一种更好的方式让我失踪,它是什么? 非常感谢。

你为什么要用“strncpy”? 你可以通过newpara [i + 1] = newpara [i + 2]做一个简单的替换; Newpara第[i + 2] = 'E';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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