Character Arrays
Objective #1: Understand various functions that can be used with built-in
C++ character arrays.
- To find a C string's (character array) actual length, you can use the strlen function. The statement,
stringLength = strlen(userString);
stores the actual length (number of printable characters) of the character
array, userString, in the numeric variable, stringLength. Note that even though
a character array may have been declared to a certain size, the string may
not use up all of those character positions. That is, in the statement,
char userName[90] = "John";
only four of the 90 positions are used by printable characters and, of course,
the null terminator takes another space. So, the length of userName is 4.
- To compare two character array in order to see if they are exactly the same,
you can use the strcmp function. So, for example,
if (strcmp(string1, string2) = = 0)
{
cout << "The strings are the same!\n";
}
causes the message to be printed on the screen if the two character arrays,
string1 and string2, are equivalent. The strcmp function returns a negative
value if the first character array is alphabetically less than the second
and it returns a positive value if the second character array is alphabetically
less than the first one. It returns a zero if and only if the two character
arrays are exactly the same.
- To combine two character arrays into one larger string is called "concatenation".
To concatenate two character arrays, you can use the strcat function. If the character array, firstName, is "John" and the character
array, secondName, is "Doe" then
strcat(firstName, secondName);
results in firstName becoming "JohnDoe" and secondName remains "Doe".
Note that in order to include a space between the two parts of the name, you
may need to do something like this:
strcat(firstName, " ");
strcat(firstName, secondName);
- In order to use strlen, strcmp, and strcat, you must include the string.h library in a compiler directive at the top of your program.
Objective #2: Use pointers with character arrays.
- As we have stated before, a character array in C++ is actually a pointer
constant that points to the first character in the character array. You'll
note that the dereferencing operator (*) is not used with the name of the
character array because it is a pointer constant and not
a pointer variable. That is, after the declaration,
char stateAbbrev[3] = "PA";
has been made the pointer constant, stateAbbrev, cannot be reassigned to point
to anything besides the 'P' even with something like the statement, stateAbbrev
= "MD".
Objective #3: Use subscript notation with built-in
C++ character arrays.
- While a string variable or a built-in C++ character array is stored in continguous
memory locations, C++ numbers the individual characters in the array beginning
with index position 0. So after the declaration statement,
char stateName[11] = "New Jersey";
or
string stateName = "New Jersey";
is made, the 'N' is considered to be in the index position 0, the first
'e' in the index position 1, the 'w' in the index position 2, and so on. Note
that the index position 3 contains a blank space, which is, of course, a valid
character. In the case of built-in, C++ character arrays, the null terminator
('\0') which marks the end of the character array, is located in the index
position 10.
- You may use subscript
notation to change the value of one specific character
within a character array or a string variable. The statement,
stateName[1] = 'E';
would change the character array or string variable to "NEw Jersey".
- The dereferencing operator (*) can be used to change the contents of a character
in a character array. (This will not work with string variables though.) The
statement,
*(stateName + 6) = 'R';
changes the character array to "NEw JeRsey", since the 'r' was in
the index position 6 and is now replaced with 'R'.