如何修复此代码的限制

how to fix the limitation of this code

本文关键字:代码 何修复      更新时间:2023-10-16

在这段代码中,我想将一个单词搜索到字符串行中,如果找到了单词,我想反转该单词。例如:This is c++ language这是字符串行,我想知道单词c++是否存在。如果存在,则打印该行CCD_ 3。所以在这一行中,单词从c++变成了++c。我该怎么做。

# include<iostream>
using namespace std;
int main() {
    int i, j,flage=0,count=0;
    char a[] = { "This is  bangladesh." };
    char b[788];
    cout<<"Before searchingn";
    cout<<"****************************"<<endl;
    for (int m=0; a[m]!='';m++) 
    {
        cout<<a[m];
    }
    cout<<endl;
    cout<<"Write what you want to search from above line: ";
    cin>>b;
    cout<<endl;
    cout<<"after searching n";
    cout<<"*****************************"<<endl;
    for (int p=0; b[p]!='';p++) 
    {
        count++;
    }
    for (i = 0; a[i] != ''; i++)
    {
        for (j = 0; b[j] != ''; j++)
        {
            if (a[j + i] != b[j])
            break;
        }
        if (b[j] == '')
            flage = 1;
    }
    if (flage == 1)
    {
        cout << "This is a ";
        for (i =count; i >= 0; i--)
            cout << b[i];
    }
    cout<<"nnnn";
    return 0;
}

我认为这是一项学习任务,所以让您使用现有函数是不可能的。。。

考虑到这一点,将您的问题划分为多个函数:首先,你需要找到你要找的单词的起始索引。找到起始索引后,找到最后一个索引(这应该很容易)现在,开始交换:将第一个索引的内容与最后一个索引交换,将第一个向前移动一步,将最后一个向后移动一步并再次交换。这样做,直到你交换了所有的单词。

如果可以的话,还有一件事,给你的变量更多的描述性名称。"a"answers"b"并不是真正的描述性。。。

试试这个

void reverse(char *x, int begin, int end)
{
        char c;
        if(begin>=end)return;
        c=*(x+begin);
        *(x+begin)=*(x+end);
        *(x+end)= c;
        reverse(x, ++begin, --end);
}
int main()
{
        int i, j,flage=0,count=0;
        char a[] = { "This is bangladesh." };
        char b[788];
        cout<<"Before searchingn";
        cout<<"****************************"<<endl;
        cout<<a;
        cout<<endl;
        cout<<"Write what you want to search from above line: ";
        cin>>b;
        cout<<endl;
        cout<<"after searching n";
        cout<<"*****************************"<<endl;
        char *ptr=strstr(a,b);
        // in "This is bangladesh" if you search for "is" it will come twice.. so check for word
        while(ptr && *(ptr-1)!=' ') //search for word
        {
          ptr+=strlen(b);
          ptr=strstr(ptr,b);
        }
        if(ptr)
        {
                reverse(b,0,strlen(b)-1);
                cout<<"string foundn";
                memcpy(ptr,b,strlen(b));
                cout<<a;
        }
        else
                cout<<"string not foundn";
        cout<<"nnnn";
        return 0;
}
# include<iostream>
using namespace std;
int main() {
    int i, j,flage=0,count=0;
    char a[] = { "This is  bangladesh." };
    char b[788];
    cout<<"Before searchingn";
    cout<<"****************************"<<endl;
    for (int m=0; a[m]!='';m++) 
    {
        cout<<a[m];
    }
    cout<<endl;
    cout<<"Write what you want to search from above line: ";
    cin>>b;
    cout<<endl;
    cout<<"after searching n";
    cout<<"*****************************"<<endl;
    for (int p=0; b[p]!='';p++) 
    {
        count++;
    }
    for (i = 0; a[i] != ''; i++)
    {
        for (j = 0; b[j] != ''; j++)
        {
            if (a[j + i] != b[j])
            break;
        }
        if (b[j] == '')
            flage = 1;
    }
    if (flage == 1)
    {
        cout << "This is a ";
        for (i =count; i >= 0; i--)
            cout << b[i];
    }
   else //Reverse the string
    {
    char *p = b[i], *s = b[i] + strlen(b[i]) - 1;
    while (p < s) {
        char tmp = *p;
        *p++ = *s;
        *s-- = tmp;
     }
   }
    cout<<"nnnn";
    return 0;
}