gpt4 book ai didi

c++ - 经期去除后的双倍空间

转载 作者:行者123 更新时间:2023-11-30 02:04:44 24 4
gpt4 key购买 nike

People who learned how to type before word processors often add two spaces after a period ending a sentence. Write a function singleSpaces that accepts a string and returns that string with all occurrences of two spaces after a "." into changed single spaces.)

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

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

最佳答案

在你的作业中有关于点后双空格的讨论,而不是文本中的所有双空格。所以你应该修改你的代码,让它

  • 等待 '.' 而不是 ' ',
  • 当'.'被拦截然后添加它,之后添加任何单个空格

您可以将此代码视为两个状态机:

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

状态 2 - 是 '.' 时找到了,在这种状态下你使用不同的代码,你添加“。”结果和 ater 正好是一个空格(如果找到的话)

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

[edit] - 用修改提示替换源代码

关于c++ - 经期去除后的双倍空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com