Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

An Incremental String Search in C


Since base and arr point to data objects of type STR, we can say that base[0] = arr[0], base[1] = arr[1] , and so on. Now suppose the user enters `B' as the first character in the search string. A simple linear search would tell us that variables base[1] and base[2] provide partial matches. We'd like to arrange things so that when the user chooses the next character in the search string, the same linear search routine will be able to tell us the new candidates for the search. We can do this if we make base point to the second character in arr[1] and restrict the maximum index for base to 1, as shown in Figure 2.

Figure 2: Pointer configuration after entering code>.

If the user enters `L' as the search character, we can apply linear search to base[0] through base[1] and determine that a match results only for index 1. This terminates the search in this example. In effect, we're using base as a movable template for our string search. Each time a search character is entered, the range of indices that yield a match is computed and the value of base is shifted accordingly.

Three points bear mentioning. One is that the algorithm depends upon representing the string to be searched as a two-dimensional array of characters. We're assuming that characters in the strings are stored in contiguous memory locations.

Second, base points to a data object of size six. This ensures that base[0], base[1], and so on are offset from one another by six bytes, so the relative offsets that apply to elements of the array arr are preserved.

Finally, the strings referenced by the base pointers in Figures 1 and 2 are in alphabetical order. This is no accident. It's a consequence of the fact that entries in arr are in sorted order and we're limiting the offsets that are added to base. If our search algorithm had referenced base[2] in Figure 2 (a pointer to the string "ANE"), then base[0] through base[2] would not be in sorted order. Part of the housekeeping the incremental search algorithm must perform involves knowing what range of offsets we can safely add to base to preserve sorted order.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.