代码之家  ›  专栏  ›  技术社区  ›  Galaxy6789

用另一个词替换句子中的一个词

  •  0
  • Galaxy6789  · 技术社区  · 6 年前

    我正在研究这个问题,但其中有一些奇怪的错误。问题出在“Replace”(更换)功能中。我已经在下面的代码中对问题进行了注释。

    我制作了三个动态字符数组(句子、单词1、单词2)并使用了cin。getline输入。我想做的是,如果:

    句子=“我喜欢比萨饼”,

    word1=“like”,以及

    word2=“仇恨”

    然后我想要一句话=“我讨厌比萨饼”。

    此外,这是我第一次使用堆栈溢出,所以如果这个线程有任何问题,请让我知道。非常感谢您的帮助。

     void Replace(char* s, char* w1, char* w2)
        {
            int lisw = 0; //lisw = letters in single word
            bool found = false;
    
            for (int i = 0; s[i] != '\0' || found == true; i++)
            {
                lisw = 0;
    
                //Problem is down here. The loop doesn't terminate when encountering a
                //space character. When i used static_cast code to check the ASCII 
                //values only junk values were output. If i just cout<<s; then there is
                //no problem but doing it here causes some weird logical errors.
                for (int j = i; s[j] != ' '; j++)
                {
                    lisw++;
                    cout << static_cast<int>(s[j]);
                }
    
                found = true;
    
                for (int j = i; j < lisw; j++)
                {
                    if (s[j] != w1[j])
                    {
                        found = false;
                    }
    
                }
    
                if (found == true)
                {
                    for (int j = i; j < lisw; j++)
                    {
                        s[j] = w2[j];
                    }
                }
    
                i = i + lisw;
            }
        }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Galaxy6789    6 年前

    好的,多亏了这些评论,我找到了一个解决方案。我将复制下面更正的代码。可以在评论中看到更正。

     void Replace(char* s,char* w1, char* w2)
        {
            int lisw = 0; //lisw = letters in single word
            bool found =false;
    
    
            for (int i=0;s[i]!='\0' && found!=true;i++) //Replaced || with &&.
            {
                lisw=0;
    
                for (int j=i;s[j]!=' '&& s[j]!='\0';j++)//added: && s[j]!='\0' so the loop terminates because otherwise it kept finding "spaces" since the character array was of a larger size to accommodate any sentence. 
                {
                    lisw++;
    
                }
    
                found=true;
    
                int k=0;
    
                for (int j=i;j<lisw+i;j++) //loop goes until lisw+i instead of lisw 
                {
    
    
                    if (s[j]!=w1[k])
                    {
    
                        found=false;
                    }
    
                    k++;
    
                }
    
                k=0;
    
                if (found==true)
                {
                    for (int j=i;j<lisw+i;j++)//same as above.
                    {
                        s[j]=w2[k];
                        k++;
    
                    }
                }
    
                i=i+lisw;
            }
        }