如何修复此代码的限制

时间:2013-12-04 05:00:47

标签: c++

在这段代码中,我想将一个单词搜索到一个字符串行中,如果找到该单词,我想反转该单词。例如:This is c++ language这是字符串行,我想知道单词c++是否存在。如果存在,则打印此行This is ++c language。所以在这一行中,这个词从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 searching\n";
    cout<<"****************************"<<endl;

    for (int m=0; a[m]!='\0';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]!='\0';p++) 
    {
        count++;
    }

    for (i = 0; a[i] != '\0'; i++)
    {
        for (j = 0; b[j] != '\0'; j++)
        {
            if (a[j + i] != b[j])
            break;
        }
        if (b[j] == '\0')

            flage = 1;
    }

    if (flage == 1)
    {
        cout << "This is a ";
        for (i =count; i >= 0; i--)
            cout << b[i];
    }

    cout<<"\n\n\n\n";

    return 0;

}

3 个答案:

答案 0 :(得分:2)

我认为这是一项学习任务,所以让你使用现有的功能是不可能的......

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

如果可以的话还有一件事。为变量提供更具描述性的名称。 “a”和“b”并不是真正具有描述性的......

答案 1 :(得分:1)

试试这个

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 searching\n";
        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 found\n";
                memcpy(ptr,b,strlen(b));
                cout<<a;
        }
        else
                cout<<"string not found\n";
        cout<<"\n\n\n\n";
        return 0;
}

答案 2 :(得分: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 searching\n";
    cout<<"****************************"<<endl;

    for (int m=0; a[m]!='\0';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]!='\0';p++) 
    {
        count++;
    }

    for (i = 0; a[i] != '\0'; i++)
    {
        for (j = 0; b[j] != '\0'; j++)
        {
            if (a[j + i] != b[j])
            break;
        }
        if (b[j] == '\0')

            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<<"\n\n\n\n";

    return 0;

}