C++ 使用Strncpy在C++;
因此,对于我的计算机科学162课,我们被分配了创建一个简单的文本编辑器的任务——但是我们只允许使用cstring/字符数组,所以不允许使用字符串。但是,我们可以使用cstring类来执行某些函数。文本编辑器必须修复小错误,例如:如果句点后只有一个空格,请添加第二个空格;如果一个简单的单词如“the”拼写错误(如“teh”),则自动更正;如果一个句子的开头字母没有大写,请将其大写。现在,我得到了修复空间的功能,但检查“teh”并将其更改为“the”的功能让我大吃一惊。以下是我目前的计划:C++ 使用Strncpy在C++;,c++,c,C++,C,因此,对于我的计算机科学162课,我们被分配了创建一个简单的文本编辑器的任务——但是我们只允许使用cstring/字符数组,所以不允许使用字符串。但是,我们可以使用cstring类来执行某些函数。文本编辑器必须修复小错误,例如:如果句点后只有一个空格,请添加第二个空格;如果一个简单的单词如“the”拼写错误(如“teh”),则自动更正;如果一个句子的开头字母没有大写,请将其大写。现在,我得到了修复空间的功能,但检查“teh”并将其更改为“the”的功能让我大吃一惊。以下是我目前的计划: ente
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();
输入_段落(字符段落[])
{
你为什么要用“strncpy”?
您可以通过使newpara[i+1]=newpara[i+2]进行简单替换;
新段落[i+2]='e';检查空格实际应该做什么?如果您只能使用Cit检查的结构和函数来查看是否存在用户在句子末尾的句点后只放置一个空格的情况,则应将其标记为C。如果有,则会添加第二个空格,以便句点后始终有两个空格。Don不过别担心,它已经可以工作了。你有基本的设计缺陷,因为你必须向函数传递一个字符串,让它返回修改后的字符串,或者传递一个有足够空间供你修改的字符串。你现在拥有的函数不能工作!我正式觉得自己很愚蠢。非常感谢!不要对自己苛刻,伙计。我建议你阅读一些基本方法的实现。看看它们在后台是如何工作的。最后,都是关于基础的;)