开发者

BinarySearch - Replace a string element with another string element once found

How do I make a binary search replace an element of an array of strings with another element of an array of strings? This is the final part of my program and I don't get it...I know with char's you use strcpy, or .replace, etc.

I have a struct that has an "orginalWord" and a "replacementWord", then a string array of "inputword"'s, it takes the inputword, finds it within the struct array (comparing to the orginalWord), and once found (Which it does find it...that part works, it finds the correct element number, so I know the searching is correct) replaces the "inputword" with the "replacementword". I just can't make it replace the element within the "inputword" with the "reaplcementWord" it finds. Help please!! I'll post the Binarysearch function seperate from the rest of my code for easy of reading. I know this should be simple, but I can't remember for the life of me.

Example: So...searchitem is inputword[20] = "Like". Sruct Array... encryption[50].OrginalWord = "Like". Match found...encryption[50].ReplacementWord = "Ducks". I would like to put "Ducks" into inputword[20]. How would I do this using the BinarySearch?

//Function call within main:
for(number=0; number < plaincount; number++)
        { BinarySearch(encryption, count, inputword[number]);
         } 

int BinarySearch (StringPair correctword[], int size, string searchitem)
{   
    int middle =0, start = 0, last = size-1;
    bool found = false;
    int position = -1;
    while (!found && start <= last)
    { 
        middle = (start + last)/2; // Midpoint // Was a breakpoint here, why?
        if(correctword[middle].orginalWord == searchitem)
        {               
            position = middle;
            cout << "Replacing word: " << searchitem << " With: " <&l开发者_开发知识库t; position << endl;

            searchitem.swap(correctword[position].replacementWord);

            found = true;
            return position; // Return new value for inputword array?
        }
        else if (correctword[middle].orginalWord < searchitem)
            { start = middle+1; }
        else last = middle-1;
    }
    cout << "Misspelled word found: " << searchitem << endl;
    return false;
}


If searchitem is going to be modified, you need to pass it by reference:

int BinarySearch (StringPair correctword[], int size, string &searchitem)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜