尝试替换字符串中的单词

发布于 2024-12-29 18:32:36 字数 1088 浏览 3 评论 0原文

我试图从输出中获取单词并找到其中包含字母 Q 的任何单词。如果有的话,需要用“坏”这个词来代替。之后,我尝试将每个单词附加到output2。我在做这件事时遇到了麻烦。我编译时得到的错误是:

从“const char*”到“char”的无效转换[-fpermissive]

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
string manipulate(string x);
int main(int argc, char* argv[])
{
string input, temp, output, output2, test, test2;
int b;
cout << "Enter a string: ";
getline(cin, input);
istringstream iss(input);
while (iss >> test)
{  
      if(test.length() != 3)
      {
        test.append(" ", 1);   
        output.append(test);
      }
}

istringstream iss2(output);
while (iss2 >> test2)
{
      for(int i = 0; i<test2.length(); i++) 
   {
     switch(test2[i])
      {
           case 'q':
           test2[1]="bad";
           output2.append(test2);
           break;
      }

   }
}
cout << "Your orginal string was: " << input << endl;
cout << "Your new string is: " << output2 << endl;
cin.get();
return 0;
}

I am trying to take the words from output and find any word with the letter Q in it. If the word does, it needs to be replaced by the word "bad". After that, I am trying to append each word to output2. I am having trouble doing this. The error I get when I compile is:

invalid conversion from 'const char*' to 'char' [-fpermissive]

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
string manipulate(string x);
int main(int argc, char* argv[])
{
string input, temp, output, output2, test, test2;
int b;
cout << "Enter a string: ";
getline(cin, input);
istringstream iss(input);
while (iss >> test)
{  
      if(test.length() != 3)
      {
        test.append(" ", 1);   
        output.append(test);
      }
}

istringstream iss2(output);
while (iss2 >> test2)
{
      for(int i = 0; i<test2.length(); i++) 
   {
     switch(test2[i])
      {
           case 'q':
           test2[1]="bad";
           output2.append(test2);
           break;
      }

   }
}
cout << "Your orginal string was: " << input << endl;
cout << "Your new string is: " << output2 << endl;
cin.get();
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2025-01-05 18:32:36

有更简单的方法可以做到这一点:

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

int main()
{
    string s("Your homework is bad. Really bad.");

    while (s.find("bad") != string::npos)
        s.replace(s.find("bad"), 3, "good");

    cout << s << endl;

    return 0;
}

输出:

Your homework is good. Really good.

但要注意针是新值的子字符串的情况。在这种情况下,您可能需要移动索引以避免无限循环;示例:

string s("Your homework is good. Really good."),
       needle("good"),
       newVal("good to go");

size_t index = 0;

while ((index = s.find(needle, index)) != string::npos) {
    s.replace(index, needle.length(), newVal);
    index += newVal.length();
}

cout << s << endl;

输出

Your homework is good to go. Really good to go.

There is much easier way how to do that:

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

int main()
{
    string s("Your homework is bad. Really bad.");

    while (s.find("bad") != string::npos)
        s.replace(s.find("bad"), 3, "good");

    cout << s << endl;

    return 0;
}

output:

Your homework is good. Really good.

But watch out for case when the needle is a substring of the new value. In that case you might want to be shifting the index to avoid an infinite loop; example:

string s("Your homework is good. Really good."),
       needle("good"),
       newVal("good to go");

size_t index = 0;

while ((index = s.find(needle, index)) != string::npos) {
    s.replace(index, needle.length(), newVal);
    index += newVal.length();
}

cout << s << endl;

outputs

Your homework is good to go. Really good to go.
玩世 2025-01-05 18:32:36

这是编译错误的原因:

test2[1]="bad";

test2[1] 的类型为 char"bad" 的类型为 const char *:此分配不合法。

使用 std::string::replace()q 更改为 "bad"

test2.replace(i, 1, "bad");

因为您也只需要替换第一次出现的 'q' (我认为这是基于关于 for 循环中的逻辑)您可以将 for 循环替换为:

size_t q_idx = test2.find('q');
if (std::string::npos != q_idx)
{
    test2.replace(q_idx, 1, "bad");
    output2.append(test2);
}

编辑:

要替换整个单词:

test2 = "bad";

注意,output2 将包含包含 'q' 的单词当前的逻辑。这是纠正它的一种方法:

output2.append(std::string::npos != test2.find('q') ? "bad" : test2);

This is the cause of the compilation error:

test2[1]="bad";

test2[1] is of type char and "bad" is of type const char*: this assignment is not legal.

Use std::string::replace() to change q to "bad":

test2.replace(i, 1, "bad");

As you also only require to replace the first occurrence of 'q' (I think this based on the logic in the for loop) you could replace the for loop with:

size_t q_idx = test2.find('q');
if (std::string::npos != q_idx)
{
    test2.replace(q_idx, 1, "bad");
    output2.append(test2);
}

EDIT:

To replace the whole word:

test2 = "bad";

Note, output2 will contain words if they contain 'q' with the current logic. This would be one way of correcting it:

output2.append(std::string::npos != test2.find('q') ? "bad" : test2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文