Picking it Apart

One of the things that makes strings such a useful type of array is that the standard library includes a number of functions that allow us to do things to strings. If we want to use these functions, we should include the line
#include <string.h>
at the top of our file. However, we don't need to link to a string library as we do with the math library and the -lm option.

There are quite a few such functions, but we'll only look at a few of them here. We'll give a short description of each function first.

strcat()
takes two strings as arguments and copies the second string onto the end of the first string. After a call to strcat() the first string will be longer that it was before.
strcpy()
takes two strings as arguments and copies the second on top of the first. The contents of the first string are lost after a call to strcpy() .
strlen()
takes a single string as an argument and returns the length of the string. Namely, it returns the number of characters up to but not including the null byte.
strcmp()
takes two strings as arguments and compares them. If the two strings are equal, then strcmp() returns 0, if the first string comes before the second in a lexicographic order, then it returns a negative number and it returns a positive number otherwise. Lexicographic order is much like alphabetical order except that the digits all come before all the letters and the uppercase letters all come before the lower case letters. strcmp() can only be used to test for alphabetical order if the words are all of the same case convention, such as all lower case or all have only the first letter capitalized.
As we mentioned earlier, there are many others including one ( strtok() ) that helps to split a string into parts like words. Issue the command:
man string
for a complete list of those available on our system.

Implementation of strlen()

Most of the string functions are relatively simple. To illustrate this, we will write our own version of strlen() . The basic strategy is to count the number of characters we have before the null byte. Since the array is numbered starting at 0, this will also be the index of the place in the array where the null byte is stored. So we can just search the array for the null byte using the same type of search we've already seen and the just return the index.
int strlen(char s[])
{
   int i;
   for(i = 0; s[i] != '\0'; ++i);
   return i;
}

Notice that our second expression in the for is not just a test on i . It is a test on the ith character in the array. If you find this implementation confusing, then look at the following equivalent (but more verbose) implementation:
int strlen(char s[])
{
   int i;
   i = 0;
   while(s[i] != '\0')
      ++i;
   return i;
}

Or we could do something like a mid-test loop and break out when we find the null byte.
int strlen(char s[])
{
   int i;
   i = 0;
   while(1) {
      if(s[i] == '\0')
         return i;
      ++i;
   }
}

The point of showing all these different methods of implementing strlen() is to illustrate two points. First many problems have several solutions any one of which will be satisfactory. Which solution we choose will depend heavily on our prior experience and on considerations regarding the project as a whole. But this begins to get into some rather large areas of Computer Science which we cover in more detail in later courses. The other point that deserves mention is that we generally don't know exactly how someone else has solved the problem when we use their function in our program. So if we include a line like:
x = strlen(name);

in our program, we don't know exactly how the author of our strlen() implemented it. All we know is that it takes our string as input and returns to us its length. This is part of the power of abstraction. We can often use a function without knowing what goes on inside but instead by abstracting ourselves away from the details to look only at the interface. Of course, this only goes so far. While the casual driver may get along pretty well without knowing anything about how a car works, you won't win at Indy if you don't have a pretty good feel for that sort of thing.
Write an if statement (just the first line, not the body) that tests to see if the string named target is equal to the name of student number n in the array:
char names[1400][30];