Note that there are some explanatory texts on larger screens.

plurals
  1. POBinarySearch - Replace a string element with another string element once found
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>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?</p> <pre><code>//Function call within main: for(number=0; number &lt; 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 &amp;&amp; start &lt;= last) { middle = (start + last)/2; // Midpoint // Was a breakpoint here, why? if(correctword[middle].orginalWord == searchitem) { position = middle; cout &lt;&lt; "Replacing word: " &lt;&lt; searchitem &lt;&lt; " With: " &lt;&lt; position &lt;&lt; endl; searchitem.swap(correctword[position].replacementWord); found = true; return position; // Return new value for inputword array? } else if (correctword[middle].orginalWord &lt; searchitem) { start = middle+1; } else last = middle-1; } cout &lt;&lt; "Misspelled word found: " &lt;&lt; searchitem &lt;&lt; endl; return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload