Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 双间隔后周期去除器_C++_String - Fatal编程技术网

C++ 双间隔后周期去除器

C++ 双间隔后周期去除器,c++,string,C++,String,在文字处理器之前学会打字的人通常在句子结尾的句点后加上两个空格。编写一个函数singleSpaces,该函数接受一个字符串,并将该字符串以及“.”之后出现的两个空格全部返回到已更改的单个空格中。) 这就是我所拥有的;我做错了什么 #include <cmath> #include <iostream> using namespace std; string forceSingleSpaces1 (string s) { string r = "";

在文字处理器之前学会打字的人通常在句子结尾的句点后加上两个空格。编写一个函数singleSpaces,该函数接受一个字符串,并将该字符串以及“.”之后出现的两个空格全部返回到已更改的单个空格中。)

这就是我所拥有的;我做错了什么

#include <cmath>
#include <iostream>
using namespace std;



string forceSingleSpaces1 (string s) {
    string r = "";
    int i = 0;
    while (i < static_cast <int> (s.length()))  {
        if (s.at(i) != ' ')  {
            r = r + s.at(i);
            i++;
        } else  {
            r +=  ' ';
            while (i < static_cast <int> (s.length()) && s.at(i) == ' ')
                i++;
        }
    }
    return r;
}
#包括
#包括
使用名称空间std;
字符串力SingleSpaceS1(字符串s){
字符串r=“”;
int i=0;
而(i
在你的作业中,有人提到点后的双空格,而不是文本中的所有双空格。因此,您应该修改代码,以便

  • 等待“.”而不是“”
  • 当截取“.”时,添加它,然后添加任何单个空格
您可以将此代码视为两种状态机器:

状态1-当您在任何非“.”字符上循环时,在此状态下,您的代码将添加到它找到的所有结果中

状态2-找到“.”时,在这种状态下,您使用不同的代码,将“.”添加到结果中,然后再添加一个完全单一的空间(如果找到任何一个空间)

这样你就可以把你的问题分成两个子问题


[编辑]-将源代码替换为修改提示

在您的作业中,有人提到点后的双空格,而不是文本中的所有双空格。因此,您应该修改代码,以便

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

//1. loop through the string looking for ".  "
//2. when ".  " is found, delete one of the spaces
//3. Repeat process until ".  " is not found.  

string forceSingleSpaces1 (string str) {
    size_t found(str.find(".  "));
    while (found !=string::npos){
        str.erase(found+1,1);
        found = str.find(".  ");
    }

    return str;
}

int main(){

    cout << forceSingleSpaces1("sentence1.  sentence2.  end.  ") << endl;

    return EXIT_SUCCESS;
}
  • 等待“.”而不是“”
  • 当截取“.”时,添加它,然后添加任何单个空格
您可以将此代码视为两种状态机器:

状态1-当您在任何非“.”字符上循环时,在此状态下,您的代码将添加到它找到的所有结果中

状态2-是找到“.”的时间,在此状态下,您使用不同的代码,将“.”添加到结果中,并完全添加到单个空格中(如果找到任何空格)

这样你就可以把你的问题分成两个子问题

[编辑]-用修改提示替换源代码
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

//1. loop through the string looking for ".  "
//2. when ".  " is found, delete one of the spaces
//3. Repeat process until ".  " is not found.  

string forceSingleSpaces1 (string str) {
    size_t found(str.find(".  "));
    while (found !=string::npos){
        str.erase(found+1,1);
        found = str.find(".  ");
    }

    return str;
}

int main(){

    cout << forceSingleSpaces1("sentence1.  sentence2.  end.  ") << endl;

    return EXIT_SUCCESS;
}
#包括 #包括 使用名称空间std; //1. 在字符串中循环查找“” //2. 找到“.”后,删除其中一个空格 //3. 重复此过程,直到找不到“.”。 字符串力SingleSpaceS1(字符串str){ 找到的大小(str.find(“.”); while(找到!=string::npos){ str.erase(发现+1,1); found=str.find(“.”); } 返回str; } int main(){ 不能包含 #包括 #包括 使用名称空间std; //1.在字符串中循环查找“” //2.找到“.”时,删除其中一个空格 //3.重复此过程,直到找不到“.”。 字符串力SingleSpaceS1(字符串str){ 找到的大小(str.find(“.”); while(找到!=string::npos){ str.erase(发现+1,1); found=str.find(“.”); } 返回str; } int main(){ cout您可以(重新)使用一个更通用的函数,用另一个字符串替换字符串中给定字符串的出现处,如所述

#包括
#包括
void replace_all(std::string&str,const std::string&from,const std::string&to){
大小\u t开始位置\u位置=0;
while((start_pos=str.find(from,start_pos))!=std::string::npos){
str.replace(起始位置,从.length()到);
start_pos+=to.length();//如果“to”包含“from”,就像用“yx”替换“x”
}
}
int main(){
std::string text=“我老了。我用了两个空格。句点之后。”;
std::string newstyle_text(文本);
全部替换(新闻样式文本,“.”,“);
std::cout您可以(重新)使用一个更通用的函数,该函数用另一个字符串替换字符串中给定字符串的出现,如前所述

#包括
#包括
void replace_all(std::string&str,const std::string&from,const std::string&to){
大小\u t开始位置\u位置=0;
while((start_pos=str.find(from,start_pos))!=std::string::npos){
str.replace(起始位置,从.length()到);
start_pos+=to.length();//如果“to”包含“from”,就像用“yx”替换“x”
}
}
int main(){
std::string text=“我老了。我用了两个空格。句点之后。”;
std::string newstyle_text(文本);
全部替换(新闻样式文本,“.”,“);

std::cout
在文字处理器之前学会打字的人通常在句子结尾的句点后加上两个空格
与你的问题不相关,但我被告知这是英国英语中使用的一种风格。将其写成两个循环会让人更难思考。这是允许的,但不寻常。试着只在一个“while{}”中进行循环,但跟踪最近发生的事情……至少这是一种方法。(虽然,只是阅读,我觉得还可以。:-)当问你做错了什么时,如果你包括四个或五个简短的测试输入及其相应的输出,以及你期望得到的输出,这将非常有帮助。你可能还希望研究如何使用来解决这个问题,这将节省你自己的循环,但可能超出你的家庭作业范围。此外,你还需要应该通过引用传递
std::string
s(
&
)为了防止不必要的复制。@Johnsyweb要使用
删除\u copy\u如果
你必须创建一个跟踪前两个字符的函子。
在文字处理器之前学会如何打字的人通常会在句子结尾的句点后加上两个空格
这与你的问题无关,但我被告知这是一种在语言中使用的风格英式英语。把它写成两个循环会让你更难思考。这是允许的,但不寻常。试着只在一个“while{}”循环中做,但要跟踪最近发生的事情……至少这是一种方法
#include <string>
#include <regex>
#include <iostream>

int main() {
    std::string text = "I'm old.  And I use two spaces.  After periods.";
    std::regex regex = ".  ";
    std::string replacement = ". ";
    std::string newstyle_text = std::regex_replace(text, regex, repacement);

    std::cout << newstyle_text << "\n";

    return 0;
}