怎样将英文句子中出现给定单词的前后加上一个空格后输出?

【问题描述】
给定一个英文单词和一段英文句子,假设该单词和英文句子中只包含英文字母。编写一个程序,将英文句子中出现给定单词的前后加上一个空格后输出,以实现断词。要求若单词出现在句子的开头,则此单词之前不能添加空格;若单词出现在句子结尾,则此单词之后也不能添加空格;若单词在句子中连续出现,则单词与单词之间也只用一个空格断开。在句子中查找单词时大小写无关。
【输入形式】
先从控制台输入一英文单词(长度不超过10),然后在下一行输入一条英文句子(长度不超过50)。
【输出形式】
输出断词后的英文句子。
【输入样例】
the
Theschooltheboythethe
【输出样例】
The school the boy the the
【样例说明】
给定的单词为the,在给定的英文句子中,该单词出现了四次。第一次出现在句子开头,只在该单词后添加了一个空格,之前不能添加空格。第二次出现时前后各添加了一个空格。第三次与第四次连续出现,在第三次和第四次单词出现之间只加了一个空格,并且第四次出现在句子结尾,在其后也没有添加空格。

这题主要考察查找子字符串,可以用c,也可以c++,方法多种;
图片说明

#include <iostream>

using namespace std;

int main()
{

    string str; //子字符串
    std::cout << "pls input word"<<endl;
    std::cin >> str;
    string src_str;// = "Theschooltheboythethe"; //源字符串
    std::cout << "pls input sentence"<<endl;
    std::cin >> src_str;

    bool p = false; //是否匹配,若匹配true,否则false
    int idx = 0; //子字符串索引
    string dest_str = ""; //保存最终字符串
    for(int i=0;i<src_str.length();i++)
    {
        //对空格的处理
        if(idx >= str.length())
        {
            p = false;
            idx = 0;
            if(dest_str.end()-(str.size()) == dest_str.begin())
            {
               // std::cout << "In the first sentence and ignore it" <<endl;//在句子首位忽略它
            }
            else
            {
                if(*(dest_str.end()-(str.size()+1)) != ' ') //对已有的空格不作处理
                    dest_str.insert(dest_str.end()-str.size(),' ');
            }
            dest_str += ' ';
        }

        //检验是否匹配
        if(!p)
        {
            if(((char)tolower(str.at(idx))) == ((char)tolower(src_str.at(i)))) //根据题意,忽略大小写
            {
                ++idx;
                p = true;
            }
        }
        else
        {
            if(((char)tolower(str.at(idx))) == ((char)tolower(src_str.at(i)))) //根据题意,忽略大小写
            {
                ++idx;
                p = true;
            }
            else
            {
                p = false;
                idx = 0;
            }
        }
        dest_str += src_str.at(i);
    }
    std::cout << dest_str << std::endl;

    return 0;
}

你可以在每个这个单词前面加  字符串转数组 循环加 在转字符串