更改文本文件中的单词c ++

时间:2014-10-31 15:02:55

标签: c++

我正在尝试编写一个C ++程序,它可以更改文本文件中某些单词的所有迭代次数。我的代码写在下面。

但是,除非字符串周围有其他字符,否则它会这样做。例如," chair"更改为" table"除非在文本文件中,单词" chair"在它之后有一个句号。例如,在文本中,他坐在椅子上。","椅子"不会被取代。替换的代码部分是

if (s == argv[2]) {
      cout << argv[3];
}
else {
      cout << s; }

argv[2]是您要查找的字词,argv[3]是您想要替换它的字词,而s是我正在创建的新字符串。这是一个循环。

我哪里出错了?

3 个答案:

答案 0 :(得分:0)

我设法创建了一个可能的解决方案。

string s;   //This contains the contents of the file
string punctuation = ".!?:;, ";
string wordtofind = "chair";
string wordtochangeinto = "table";


for(int pos = -1;(pos = s.find(wordtofind, pos + 1)) != string::npos;)
{//Found an occurence of "chair"
    if(((pos != 0) 
             && (s.substr(pos - 1, 1).find_first_of(punctuation) == string::npos)) 
       || ((pos != s.lenght() - 1) 
             && (s.substr(pos + wordtofind.lenght(), 1).find_first_of(punctuation) == string::npos)))
    {//There was no punctuation mark on both sides, so we skip this find
        continue;
    }

    s.replace(pos, wordtofind.length() , wordtochangeinto); //Replace "chair" with "table"
}

cout << s;

答案 1 :(得分:0)

根据您提供的少量代码和后续评论,您的方法至少存在一些问题。第一个问题是处理每个单词周围的标点符号,第二个问题是维持行结尾。这两个问题都与一次读一个单词直接相关(即std::cin >> stringVar)。这些问题可以通过读取整行然后在输入行上进行简单搜索和替换来解决。

您肯定希望检查搜索条件之前和之后的字符,以确定它是单词的一部分还是被某些标点符号包围。根据您的要求,这可能就像使用C ++标准库中提供的std::isalphastd::isalnum之类的调用一样简单。或者,您也可以提供自定义搜索条件,如Vincent answer所示,但这取决于您的要求。

下面的代码演示了如何使用描述关键部分的注释来完成此操作。它使用std::istringstream作为输入流和硬编码针和替换字符串,但应该可以帮助您到达需要去的地方。

#include <iostream>
#include <sstream>
#include <cctype>

int main()
{
    //  `input` represents the input stream which could be your file.
    std::istringstream input(
        "The quick brown fox jumps over the lazy dog\n"
        "The quick, brown fox jumps over the lazy dog\n"
        "The xquick brown fox jumps over the lazy dog\n"
        "The quickx, brown fox jumps over the lazy dog\n"
        "The quick, brown fox jumps over the lazy dog\n"
        "The quick, brown fox jumps over the lazy dog, quick\n"
        "The quick, brown fox jumps over the lazy dog, quickly\n"
        );

    //  The following can be changed to needle(argv[2]) and newWord(argv[3])
    //  to search and replace based on strings provided on the command line
    const std::string needle("quick");
    const std::string newWord("slow");
    std::string line;
    while (std::getline(input, line))
    {
        for (
            auto pos = line.find(needle, 0);
            pos != std::string::npos;
            pos = line.find(needle, pos))
        {
            //  Get the character before the found string or space if at
            //  beginning of line.
            const char leftChar = pos > 0
                ? line[pos - 1]
                : ' ';

            //  Get the character after the found string or space if at the end
            //  of the line.
            const char rightChar = pos + needle.size() < line.size()
                ? line[pos + needle.size()]
                : ' ';

            //  Check to see is the characters before and after meet the criteria
            //  necessary to replace the string. We need to do this in order to
            //  prevent replacing substrings of a word (i.e. `it` in `smitten`.
            //  Replace std::isalpha with std::isalnum or your own function that
            //  tests for characters that do not appear in words.
            if (!std::isalpha(leftChar) && !std::isalpha(rightChar))
            {
                line.replace(pos, needle.size(), newWord);
                pos += newWord.size();
            }
            else
            {
                pos += needle.size();
            }
        }
        std::cout << line << std::endl;
    }
}

输出

  

慢棕狐跳过懒狗   慢慢的棕色狐狸跳过懒狗   xquick棕色狐狸跳过懒狗   快速的,棕色的狐狸跳过懒狗   慢慢的棕色狐狸跳过懒狗   慢慢的棕色狐狸跳过懒狗,慢了   慢慢的棕色狐狸迅速跳过懒狗,

答案 2 :(得分:-1)

if (s == argv[2])

糟糕!请使用strcmp或其相当的一部分吗?你在比较指针。

从文件中读出字符串时,您是否确保读取“主席”而不是“主席”?

在将文件写入文件时,

putline / puts是您的朋友。

相关问题