Arrays as Strings
Chapter 8
Arrays of Strings
Strings in C++ can be manipulated using either the data type string or character arrays (C-strings).
To declare an array of 100 elements of type string:
cppstring list[100];
Basic operations, such as assignment, comparison, and input/output, can be performed on values of the string type.
The data in
list
can be processed just like any one-dimensional array.
string names[100];
names[1] = "Abednego";
names[2] = "Meshach";
names[3] = "Shadrach";
C-Strings (Null-Terminated Character Arrays)
A character array is an array whose elements are of the char
.
A c-string is a null-terminated character array.
- Null-terminated means the last character in the array is null (i.e., the character
'\0'
or0
). 'A'
is achar
."A"
is a c-string. It represents an array of two characters,'A'
and'\0'
.char name[16];
is a character array that can hold a c-string with up to 15 characters and the null character.- Components after the null character (if any) are unused and ignored.
- You do not need to have
#include <string>
for c-strings.
INFO
The code for this video was written using Visual Studio, which is really nice IDE (integrated development environment) for Windows. It is installed on all the lab computers and the Community Edition is available to download for free. Go here for more information.
As with all the example code for this class, this is standard C++ code and will work with our usual compiling process.
The size (or length) of an array can be omitted if the array is initialized during declaration.
char name[] = "John";
- Declares an array of length 5 and stores the c-string
"John\0"
in it.
- Declares an array of length 5 and stores the c-string
Useful string manipulation functions from the cstring
header:
strcpy(char a[], char b[])
– copies a c-string (an unsafe operation, because the source array may not fit in the destination array and overflow into other memory locations)strncpy(char a[], char b[], int num)
– safely copies a c-string with up to num charsstrcmp(char a[], char b[])
– compares c-strings, character by character. Returns 0 if the parameters are equal, < 0 ifand > 0 if . strlen(char a[])
– returns a c-string's length (the number of characters before'\0'
)
String Comparison
Strings are compared character by character starting from the beginning. The following are true:
strcmp("Air", "Boat") < 0
strcmp("Air", "An") < 0
strcmp("Bill", "Billy") < 0
strcmp("Bill", "Bill") == 0
strcmp("Hello", "hello") < 0
Reading and Writing C-Strings
Most rules for arrays also apply to c-strings (which are null-terminated character arrays).
- Aggregate operations, such as assignment and comparison, are not allowed on arrays.
- However, C++ does allow aggregate operations for the input and output of C-strings.
Example:
cppchar name[16]; cin >> name; // Danger! Input could be more characters than fit in name[].
Use
cin.get()
is better because you can ensure that you do not overflow the array (see below for details).
To read strings with blanks, use the get
function:
int MAX_LENGTH = 16;
char name[MAX_LENGTH] // space for 15 letters + '\0'
cin.get(name, MAX_LENGTH); // read in up to 15 chars, stop at newline
cin.get(name, MAX_LENGTH, ' '); // read in up to 15 chars, stop at blank space
- Stores the next
MAX_LENGTH
characters intoname
but the newline character is not stored inname
. - If the string has fewer than
m
characters, reading stops at the newline character. - Add a third parameter to use a delimiter instead of a newline:
cin.get(str, MAX_LENGTH, ' ');
- The previous statement reads in m characters or until a blank space is reached.
To output a c-string:cout << name;
- Outputs the content of
name
on the screen <<
continues to write the contents ofname
until it finds the null character- If
name
does not contain the null character, then the output may be strange.- The output continues through memory adjacent to
name
until a'\0'
is found.
- The output continues through memory adjacent to
string
to C-string
String variables may hold filenames to be opened with file streams. However, conversion from strings to c-strings is helpful for other reasons.
Syntax:
strVar.c_str();
- Where
strVar
is a variable of type string