用数字递增替换字符串中的空格

时间:2019-04-08 23:24:31

标签: c++

我需要一个程序来接收字符串并用递增的数字替换空格。

#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

    // Get the String
    string str = "this essay needs each word to be numbered";
    int num = 1;
    string x = num;
    int i = 0;



    // read string character by character.
    for (i < str.length(); ++i) {

        // Changing the loaded character
        // to a number if it's a space.
        if (str[i] == ' ') {

            str[i] = x;
            ++num

        }
    }

    // testing outputs
    cout << str << endl;
    cout << num << endl;

  ofstream file;
  file.open ("numbered.txt");
  file << str;
  file.close();

    return 0;
}

我可以将其替换为字母或符号并将其保存到新文件,但是当我尝试为其指定数字时,它就停止了工作。我需要说“ this1essay2needs3each4word5to6be7numbered

4 个答案:

答案 0 :(得分:2)

为简便起见,请更改您的方法。

  • 将字符串放入;4 ;group 452 ;ring
  • 提取每个以空格分隔的子字符串,并将其放入istringstream
  • 将向量的内容馈送到std::vector<string>
  • 使用stringstream在子字符串之间添加数字

例如:

std::to_string(num)

答案 1 :(得分:2)

让我们将问题分解为几个部分。我们可以制作一个SpaceReplacer对象进行替换。它有一个Output,可用作输出字符的功能:

template<class Output>
struct SpaceReplacer {
    Output output; 
    int num_spaces; 
    void input(char c) {
        if(c == ' ') {
             auto num_as_string = std::to_string(num_spaces); 
             num_spaces += 1; 
             for(char digit : num_as_string) {
                 output(digit); 
             }
        }
        else {
            output(c); 
        }
    }
}; 

每次输入一个字符时,它要么输出您输入的字符,要么输出数字的数字(如果字符是一个空格)。

我们应该编写一个辅助函数来制作SpaceReplacer

template<class Output>
SpaceReplacer<Output> makeReplacer(Output output_func) {
    return SpaceReplacer<Output>{output_func, 0}; 
}

读取一个字符串,返回新字符串

现在很容易编写一个函数来替换字符串中的空格。

std::string replaceSpaces(std::string const& input) {
    std::string output_string; 
    // We output chars by appending them to the output string
    auto output_func = [&](char c) { output_string += c; }; 
    auto replacer = makeReplacer(output_func); 
    for(char c : input) {
        replacer.input(c); 
    }
    return output_string; 
}

从文件读取输入,替换空格并返回字符串

因为我们编写了一个非常通用的SpaceReplacer类,所以我们可以修改该函数,以便它可以直接从FILE*读取输入

std::string replaceSpaces(FILE* file) {
    std::string output_string;
    auto output_func = [&](char c) { output_string += c; }; 
    auto replacer = makeReplacer(output_func); 

    while(true) {
        int input_char = fgetc(file); 
        if(input_char == EOF) {
             break; 
        }
        replacer.input(input_char); 
    }
    return output_string; 
}

从一个文件中读取输入,立即将其添加到其他文件中,并替换空格

我们还可以直接从一个文件读取,并直接输出到另一个文件,而不会产生延迟。如果您要处理大量数据,这可能很有用。

void replaceSpaces(FILE* input_file, FILE* output_file) {
    auto output_func = [=](char c) { fputc(c, output_file); }; 
    auto replacer = makeReplacer(output_func); 

    while(true) {
        int input_char = fgetc(input_file); 
        if(input_char == EOF) {
             break; 
        }
        replacer.input(input_char); 
    }
}

答案 2 :(得分:2)

在这种情况下,您需要为结果使用另一个字符串。

#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

    // Get the String
    string result, str = "this essay needs each word to be numbered qwe qwe  wqe qwe qwe qwe q";
    int num = 0;
    int i;

    // read string character by character.
    for (i=0; i < str.length(); i++) {

        // Changing the loaded character
        // to a number if it's a space.
        if (str[i] == ' ') 
            result+=std::to_string(++num);
        else
            result+=str[i];
    }

    // testing outputs
    cout<<result<<endl;
    cout<<num;

    ofstream file;
    file.open ("numbered.txt");
    file << result;
    file.close();

    return 0;
}

答案 3 :(得分:-2)

您必须将其替换为字符,而不是数字。

str[i] = num+'0';