A string may occupy all or part of the space allocated for it. For example, suppose the name Brian Jurries is stored in a character array declared as char name[25];. There are actually 13 printable characters before the null terminator, even though the array is 25 characters long.
The point is that the length of the string is less than the length of the character array in which the string is stored. There are times when knowing the length of the string is important. For example, suppose you want to center a string on the screen. If you know the length of the string, you can determine the number of blank spaces that must be printed before the string in order to center it.
Writing a function to find the length of a string is easy. The following example shows a function that accepts a pointer to a character array as a parameter and returns the string's length as an integer. The parameter must be a pointer variable so that any character array may be passed to the function. The program uses a while loop to move from one character to the next in the character array and stops when the null terminator is reached. The position of the null terminator is returned as the length of the string.
=======
int stringlength(char *string_in)
{
int countlength = 0;
while(string_in[countlength] != '\0')
countlength++;
return(countlength);
}
===========
Because the character array is access beginning with the subscript
zero, the position of the null terminator is equivalent to the
length of the string. For example, a five character string would
be stored in subscripts 0 through 4 with the null terminator at
subscript 5, which is the length of the string.
There is a C++ library function named strlen which will return the length of a string. You can use strlen in the same way you used the stringlength function, with a call like this one:
i = strlen(mystirng);
The strlen function requires that the compiler directive
#include
Comparing invidivual variables in C++ is easy. For example, the
if structure below makes a decision based on whether two variables
have the same value.
The string.h library also includes a function for comparing strings.
The library function named strcmp performs a comparison of two
strings and returns one of the following values:
> it returns a zero(0) if the strings are the same
> it returns a negative value if the first string comes after the
second in alphabethical order
> it returns a positive value if the first string comes after the
second in alphabetical order
Below is an example of how the strcmp function can be used within
an in structure to determine if two strings are the same.
The library function that concatenates strings is called strcat.
The strcat function receives two strings as parameters, as shown
in the example call below.
strcat(phone_number, extension);
After the call to strcat, the string stored in phone_number will
still have all of the characters it had before the call, plus all
of the characters that are in the string stored in extension. The
string stored in extension remains the same.
The character array that is going to receive the additonal characters
must have enough memory allocated to it in order to hold the additional
characters. The strcat function will continue to add characters even
if it reaches the end of the destination character array. The result
is that other memory is overwritten with the remaining string data.
Remember that the strcat function requires strings as arguments.
Even when adding single character to a string, as in the example
below, double quotes must be used so that the blank space is
interpreted as a string rather than a character.
strcat(fullname, " ");
============
Questions: (1-5 are t/f)
1. A string always occupies the entire character array in which it
is stored.
2. Functions that compare strings compare characters one at a time.
3. The strcmp function can tell you which string comes first in
alphabetical order.
4. The strcmp function returns a zero if the strings are the same.
5. Concatenation is the term for comparing strings.
6. What library function will determine the length of a string?
7. What header file must be included with a compiler directive
in order to use the string functions?
8. What does the strcat function do with the concatenated characters
that do not fit in the character array?
Problem 12-2-1
Write a program that asks the user for a string and then prints the
string centered on the screen. Use the strlen function to determine
the length of the string and decide how many spaces must be printed
to the left of the string in order to center the string on the screen.
if (x==y)
{
cout << "x and y are equal.\n";
}
Comparing strings is not as straightforward. To determine
whether two character arrays are storing the same string, each
character of one array must be compared to each corresponding
character in the other array. Therefore, a special function must
be used to compare strings.
if (strcmp(string1, string2) == 0)
{
cout << "The strings are the same.\n";
}
Adding one string onto the end of another string is called
concatenation. Suppose that you have one character array that
is holding a person's first name, and another that is holding the
same person's last name. To get both the first and the last names
together in one character array, you would need to concatenate
the last name onto the first name. A space must be concatenated
first to insert a space between the first and last name.