#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()
strcat()
the first string will be longer
that it was before.
strcpy()
strcpy()
.
strlen()
strcmp()
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.
strtok()
) that helps to split a string into parts
like words.
Issue the command:
man stringfor a complete list of those available on our system.
strlen()
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 ) ; }
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 ) ; }
int strlen( char s[] ) { int i ; i = 0 ; while( 1 ) { if( s[i] == '\0' ) return( i ) ; ++i ; } }
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 ) ;
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.
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] ;