// you’re reading...

C++ Glossary

Using Strings in an Array

The two most common ways of getting a string into an array

  • Are to initialize an array to a string constant and to read keyboard or file input into an array
  • (A-1) demonstrates these approaches by initializing one array to a quoted string and using cin to place an input string in a second array
    • The program (A-1) also uses the standard library function strlen() to get the length of a string
    • The standard cstring header file (or string.h for older implementations) provides declarations for this and many other string-related functions (A-1)



Example of (A-1)

// strings.cpp -- storing strings in an array
#include <iostream>
#include <cstring> // for the strlen() function
int main()
{
	using namespace std;
	const int kSize = 15;
	char name1[kSize]; // empty array
	char name2[kSize] = "C++omputer"; // initialized array
	// NOTE: some implementations may require the static keyword
	// to initialize the array name2
 
	cout << "Howdy! I'm " << name2;
	cout << "! What's your name?\n";
	cin >> name1;
	cout << "Well, " << name1 << ", your name has ";
	cout << strlen(name1) << " letters and is stored\n";
	cout << "in an array of " << sizeof(name1) << " bytes.\n";
	cout << "Your initial is " << name1[0] << ".\n";
	name2[3] = '\0'; // null character
	cout << "Here are the first 3 characters of my name: ";
	cout << name2 << endl;
	return 0;
}



Compatibility Note (A-1)

  • If your system doesn’t provide the cstring header file, try the older string.h version



Here is a sample run of the program in (A-1):

Output




Program Notes



First, note that the sizeof operator gives the size of the entire array, 15 bytes, but the strlen() function returns the size of the string stored in the array and not the size of the array itself (B-1)

  • (B-1) Also, strlen() counts just the visible characters and not the null character. Thus, it returns a value of 6, not 7, for the length of Bjarne (B-2)
  • (B-2) If Stroustrup is a string, the minimum array size for holding that string is strlen(Stroustrup) + 1


 
(B-3) Because name1 and name2 are arrays, you can use an index to access individual characters in the array (B-4)

  • (B-4) For example, the program uses name1[0] to find the first character in that array (B-5)
  • (B-5) Also, the program sets name2[3] to the null character. That makes the string end after three characters, even though more characters remain in the array (Image-1)


 
(B-6) Note that the program in (A-1) uses a symbolic constant for the array size (B-7)

  • (B-7) Often, the size of an array appears in several statements in a program (B-8)
  • (B-8) Using a symbolic constant to represent the size of an array simplifies revising the program to use a different array size; you just have to change the value once, where the symbolic constant is defined



(Image-1) Shortening a string with \0

Shortening a string

Discussion

No comments for “Using Strings in an Array”

Post a comment