Previous: 9.3 Arrays of Strings
Up: 9.3 Arrays of Strings
Previous Page: 9.3 Arrays of Strings
Next Page: 9.4 Arrays of Pointers

9.3.1 String Sorting and Searching

Our next couple of tasks are simple and build on the last one. In one task we search (sequentially) for a string and in another we sort a set of strings.

SRCHSTR: Search for a key string in a set of strings.

We will use a function, srchstr(), to search for a string in an array of a specified size. The function returns either a valid index where the string is found or it returns -1 to indicate failure. The algorithm is simple enough and the implementation of a test program driver is shown in Figure 9.15.

The file strtab.h includes the prototypes for functions in file strtab.c. Observe the initialization of the two dimensional array names[][] using constant initializers written in braces separated by commas. Each initializer initializes a one dimensional string array written as a string constant. The program calls srchstrtab() searching for the string "John Smith", and prints the returned index value.

As we have seen in Chapter , the library function strcmp() is used to compare two strings. The function returns zero if the argument strings are equal. A sequential search process for strings is easily developed by modifying the sequential search function of Chapter replacing the equality operator with the function strcmp() to compare two strings.

In the function, srchstrtab(), we compare each string in the array with the desired string until we either find a match or the array is exhausted. The function call requires the name of the array of strings, the number of valid elements in the array, and the item to be searched for. For example, suppose we wish to search for a string, key, in the array, names with n string elements, then, the function call would be:

k = srchstrtab(names, n, key);
The value returned is assigned to an integer variable, k. If successful, srchstrtab() returns the index where the string was found; otherwise, it returns -1. The function is shown in Figure 9.16.

In the for loop, the string that strtab[i] points to is compared with the string that key points to. If they are equal, strcmp() returns zero and the value of i is returned by the function. Otherwise, i is incremented, and the process is repeated. The loop continues until the valid array is exhausted, in which case -1 is returned. Again, the formal parameter definition for the two dimensional array, x, requires the size of the second dimension, SIZE. A sample run of the program is shown below:

Our next task calls for sorting a set of strings.

SORTSTR: Sort a set of strings. Print strings in unsorted and in sorted order.

The algorithm is again very simple and we implement it in the program driver. The driver simply calls on sortstrtab() to sort the strings and prints the strings, first unsorted and then sorted. A prototype for sortstrtab() is included in file strtab.h and the driver is shown in Figure 9.17.

An array of strings is initialized in the declaration and the unsorted array is printed. Then, the array is sorted, and the sorted array is printed.

Sorting of an array of strings is equally straight forward. Let us assume, the array of strings is to be sorted in increasing ASCII order, i.e. a is less than b, b is less than c, A is less than a, and so on. We will use the selection sort algorithm from Chapter . Two nested loops are needed; the inner loop moves the largest string in an array of some effective size to the highest index in the array, and the outer loop repeats the process with a decremented effective size until the effective size is one. The function is included in file strtab.c and shown in Figure 9.18.

The function is similar to the numeric selection sort function, except that we now use strcmp() to compare strings and strcpy() to swap strings. A sample session is shown below:

In our example program, the entire strings are compared. If we wish to sort by last name, we could modify our function to find and compare only the last names.



Previous: 9.3 Arrays of Strings
Up: 9.3 Arrays of Strings
Previous Page: 9.3 Arrays of Strings
Next Page: 9.4 Arrays of Pointers

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:20:12 HST 1994