char label[10] = "value"; char *labelPtr = label; // Point to the 'label' array.
These would be organized (in memory) as follows:
label ------------------------------------------ +->| v | a | l | u | e | \0 | | | | | | ------------------------------------------ | | labelPtr | ------ +--+-- | ------
Then, either the original array or a pointer to it can be used to access the string's contents:
The only way to change or compare the content of arrays is to deal with each element. In other words, we can't do the following:cout << "label is " << label << endl; cout << "label is " << labelPtr << endl; cout << "3rd char is " << label[2] << endl; cout << "3rd char is " << labelPtr[2] << endl;
| Changing | Comparing |
|---|---|
char label[10] = "value"; char label2[] = "another"; char *labelPtr = label2; label = "new val"; // No! label = label2; // Wrong! label = labelPtr; // Wrong! |
char label1[] = "address";
char label2[] = "phone";
if (label1 == label2) // Not what you
// think it does!
|
label was a character pointer (instead of an array);
however, what would be happening is the pointer would be taking on the
address of a different string, which is not the same as changing the
contents of an array.
It would be annoying to have to do things like:
| Changing | Comparing |
|---|---|
char name[10]; name[0] = 'R'; name[1] = 'o'; name[2] = 'b'; name[3] = '\0'; |
char name1[] = "Dawn";
char name2[] = "Phil";
if (name1[0] == name2[0] &&
name1[1] == name2[1] &&
name1[2] == name2[2] &&
name1[3] == name2[3]) {
cout << "First 4 chars are same" << endl;
}
|
or to write loops all the time to do common string operations...
Plus, we'd probably forget the nul character (\0)
half the time.
string.h has several common functions
for dealing with strings. 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.
int StrLength(char *str);
This function should work like strlen() and return the length
of a string.
int StrCompare(char *str1, char *str2);
This functions should work like strcmp(), returning a number
representing whether the 1st string is greater than, less
than, or equal to the 2nd string.
char *StrCopy(char *dest, char *source);
This function should work like strcpy(), copying the contents
of the source string into the destination string (and returning the
destination string).
char *StrConcat(char *dest, char *source);
This function should work like strcat(), appending the contents
of the source string to the end of the destination string (and returning the
destination string).
strtest.cpp
provides a main program to test your string functions.
Compile this test program with your string functions and run it a few times to test it.