would be a simple way to create a string with an initial value. In memory, this array looks like:char name[12] = "Bob Dylan";
The only thing special about this array is that an extra character, the nul character tells us where the string's value ends.-------------------------------------------------- | B | o | b | | D | y | l | a | n | \0 | | | --------------------------------------------------
Because a string is really just an array, we must access or change it by dealing with each element (like other arrays).
This means it becomes tedious to perform common string operations, i.e.,
What kind of language construct would we use to manipulate or search through the characters of a string?
Because these are such common operations, there are library functions that do these things for us...
string.h (also referred to as
cstring) has several common functions for dealing with
strings stored in arrays of characters. Following are the 4 basic
string library functions that we'll discuss:
strlen(str)Returns the number of characters in the string, not including the nul character.
strcmp(str1, str2)This function takes two strings and compares them. If the strings are equal, it returns 0. If the first is greater than the 2nd, then it returns some value greater than 0. If the first is less than the 2nd, then it returns some value less than 0.
You might use strcmp() as in:
The ordering for strings is lexical order based on the ASCII value of characters. Remember that the ASCII value of#include <string.h> char str1[] = "garden"; if (strcmp(str1, "apple") == 0) cout << "Equal" << endl; else cout << "Not equal" << endl; OR if (strcmp(str1, "eden") > 0) cout << "'" << str1 << "' comes after 'eden'" << endl;
'A' and
'a' (i.e., upper/lowercase) are not the same.
strcmp() to compare 2
strings (let's say a and b) is to use the
following mnemonics:
Want... Use... a == b strcmp(a, b) == 0a < b strcmp(a, b) < 0a >= b strcmp(a, b) >= 0... ...
strcpy(dest, source)Copies the contents of source into dest, as in:
#include <string.h> char str1[10] = "initvalue"; strcpy(str1, "second");
Now, the string str1 contains the following:
------------------------------------------- | s | e | c | o | n | d | \0 | u | e | \0 | -------------------------------------------
and the word "initvalue" has been overwritten. Note that it is the
first nul character (\0) that determines the end
of the string.
When using strcpy(), make sure the destination
is big enough to hold the new string.
dest = source
Also, strcpy() returns the destination string, but that
return value is often ignored.
strcat(dest, source)Copies the contents of source onto the end of dest, as in:
#include <string.h> char str2[10] = "first"; strcat(str2, " one");
Now, the string str2 contains the following:
------------------------------------------ | f | i | r | s | t | | o | n | e | \0 | ------------------------------------------
When using strcat(), make sure the destination
is big enough to hold the extra characters.
strcat() also returns the destination
string, but that return value is often ignored.