There’s more to strings than reading them.
You’ve seen how strings in C are actually char
arrays but what does C
allow you to do with them? That’s where string.h comes in.
string.h is part of the C Standard Library that’s
dedicated to string manipulation. If
you want to concatenate strings
together, copy one string to
another, or compare two strings,
the functions in string.h are there to help. In this
chapter, you’ll see how to create an array of
strings, and then take a close look at how to search within strings using the strstr()
function.
There are so many tracks on the retro jukebox that people can’t find the music they are looking for. To help the customers, the guys in the Head First Lounge want you to write another program.
This is the track list:
The list is likely to get longer, so there’s just the first few tracks for now. You’ll need to write a C program that will ask the user which track she is looking for, and then get it to search through all of the tracks and display any that match.
There are several track names that you need to record. You can record several things at once in an array. But remember: each string is itself an array. That means you need to create an array of arrays, like this:
The array of arrays looks something like this in memory:
So now that you know how to record the data in C, what do you need to do with it?
The guys have helpfully given you a spec.
Ask the user for the text she’s looking for.
Loop through all of the track names.
If a track name contains the search text, display the track name.
Well, you know how to record the tracks. You also know how to read the value of an individual track name, so it shouldn’t be too difficult to loop through each of them. You even know how to ask the user for a piece of text to search for. But how do you look to see if the track name contains a given piece of text?
The C Standard Library is a bunch of useful code that you get for free when you install a C compiler. The library code does useful stuff like opening files, or doing math, or managing memory. Now, chances are, you won’t want to use the whole of the Standard Library at once, so the library is broken up into several sections, and each one has a header file. The header file lists all of the functions that live in a particular section of the library.
So far, you have only really used the
stdio.h header file. stdio.h
lets you use the standard input/output functions
like printf
and scanf
.
But the Standard Library also contains code to process strings. String processing is required by a lot of the programs, and the string code in the Standard Library is tested, stable, and fast.
You include the string code into your program using the string.h header file. You add it at the top of your program, just like you include stdio.h.
So how exactly does the strstr()
function work? Let’s look at an
example. Let’s say you’re looking for the string “fun” inside a larger
string, “dysfunctional.” You’d call it like this:
The strstr()
function will
search for the second string in the first string.
If it finds the string, it will return the address of the located string
in memory. In the example here, the function would find that the
fun
substring begins at memory
location 4,000,003.
But what if the strstr()
can’t
find the substring? What then? In that case, strstr()
returns the value 0. Can you think
why that is? Well, if you remember, C treats zero as
false. That means you can use strstr()
to check for the
existence of one string inside another, like
this:
char s0[] = "dysfunctional"; char s1[] = "fun"; if (strstr(s0, s1)) puts("I found the fun in dysfunctional!");
Let’s see how we can use strstr() in the jukebox program.
Let’s bring the code together and review what you’ve got so far:
It’s important that you assemble the code in this order. The
headers are included at the top so that the compiler will have all the
correct functions before it compiles your code. Then you define the
tracks
before
you write the functions. This is called putting the tracks
array in global
scope. A global variable is one that lives outside any
particular function. Global variables like tracks
are available to all of the functions
in the program.
Finally, you have the functions: find_track()
first, followed by main()
. The find_track()
function needs to come first,
before you call it from main()
.
For more information about the functions available in string.h, see http://tinyurl.com/82acwue.
If you are using a Mac or a Linux machine, you can find out more
about each of the string.h functions like
strstr()
by typing:
man strstr
You’ve seen how to use an array of arrays to store a sequence of strings, but another option is to use an array of pointers. An array of pointers is actually what it sounds like: a list of memory addresses stored in an array. It’s very useful if you want to quickly create a list of string literals:
You can access the array of pointers just like you accessed the array of arrays.
You’ve got Chapter 2.5 under your belt, and now you’ve added strings to your toolbox. For a complete list of tooltips in the book, see Appendix B.