用越来越多的数字替换字符串中的空格

问题描述 投票:1回答:4

我需要一个程序来取一个字符串并用越来越多的数字替换空格。

#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

c++
4个回答
2
投票

为了方便和清晰,改变您的方法。

  • 把字符串放入istringstream
  • 提取每个空格分隔的子串并放入std::vector<string>
  • 将载体的内容物喂入stringstream
  • 使用std::to_string(num)在子串之间添加数字

e.f.:

    std::string str = "this essay needs each word to be numbered";
    int num = 1;

    std::istringstream istr(str);
    std::string temp;
    std::vector<std::string> substrs;
    while (istr >> temp)
    {
      substrs.push_back(temp);
    }
    std::stringstream ostr;
    for (auto&& substr : substrs)
    {
      ostr << substr << std::to_string(num++);
    }

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

每次输入字符时,它都会输出您输入的字符,或者输出数字的数字(如果字符是空格)。

我们应该写一个辅助函数来制作SpaceReplacers:

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

Reading one string, returning new string

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

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

Reading input from file, replacing spaces and returning a 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; 
}

Reading input from one file, immediately appending it to different file with spaces replaced

我们也可以直接从一个文件中读取,并直接输出到另一个文件,没有延迟。如果您处理的是大量数据,这可能很有用。

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
投票

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

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

-2
投票

您必须用字符替换它,而不是用数字替换它。

str[i] = num+'0';
© www.soinside.com 2019 - 2024. All rights reserved.