Updated March 17, 2023
Introduction to String Array in C++
There are many data types in C++, like integer, float, character, string. The string data type is an array of characters ending with a null character (‘\0’), denoting the end of the array or string. C did not have them as such the data type string, because of which we had to form a character array to form a string. In C++, we have the inbuilt data type string.
Example of character: ‘a’ or ‘A.’
Example of string (C++): “English.”
String: array of character: String[0] = ‘E’
String[1] = ‘n’
String[2] = ‘g’
String[3] = ‘l’
String[4] = ‘i’
String[5] = ‘s’
String[6] = ‘h’
String[7] = ‘\0’
Strings can be declared, written and printed directly in C++. Also, each character in a string can be accessed using an index similar to indexing in the array. In the string’s case, when we read in the form of a character array using scanf(), it will stop the string or reading function when it finds the first white space. To avoid this gets() function can be used. This reads a whole line and will stop reading only when the user hits ‘Enter’.
String Array in C++ an array of multiple strings
String array or Array of strings is an array of multiple strings. This can be declared as follows:
string Animals[4] = {"Elephant", "Fox", "Lion", "Tiger"};
for(int i = 0; i<4; i++) {
cout << Animals[i] << endl;}
Output:
How to Access the Elements from the String Array?
The array of strings is similar to a 2-dimensional array. The first dimension or index specifies the index of string from the array-like 1st word/string or 2nd word/string and so on. Whereas the second dimension or index specifies which character in that specific word/string.
To explain it with a clear example:
Animals[2][1] = 'i'
In the above example, the first index ‘2’ specifies it is the 2nd string from the array: “Tiger”. The second index, ‘1’, specifies it is the 2nd letter or index 1 from the word “Tiger”. Using the 2dimensional indexing, each character from each string can be accessed easily.
Allocation or Defining String Array
There are different methods of allocation of an array of strings:
1. 2D Array of Char (which can be used in C also)
char Name[max number of elements][max size of each element]
char color[4][8] = {“blue”, “red”, “white”, “black”}
Code:
int main() {
char color[4][8] = {"blue", "red", "white", "black"};
for(int i = 0; i<4; i++) {
cout << color[i] << endl;
}
}
Output:
Here the array colour is of fixed size; that is, the number of elements is fixed, and also the number of characters or sizes of the element is also fixed. The maximum number of elements that can be defined is 4, and each can have a maximum of 8 characters, not more than that. This is the main barrier in the 2d character array.
2. Array with keyword String (only possible in C++)
string Name[max number of elements]
string color[4] = {“blue”, “red”, “white”, “black”}
Code:
int main() {
string color[4] = {"blue", "red", "white", "black"};
for(int i = 0; i<4; i++) {
cout << color[i] << endl;
}
}
Output:
Here only one dimension is fixed when we declare it as a string array. Only the number of elements is fixed. The size of the element can vary. But specifying the second dimension, we can access or display the specific character from a respective string.
3. Using Vectors in C++
vector Name
vector colour
Code:
#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector <string> color;
color.push_back("blue");
color.push_back("red");
color.push_back("white");
color.push_back("black");
for(int i = 0; i<color.size(); i++) {
cout << color[i] << endl;
}
return 0;}
Output:
Here it is dynamically allocated, memory is allocated from the heap. The size is not fixed. It can increase or decrease as per the number of elements. In the case of dynamically allocating string array using the command “new”, we need to deallocate the allocated memory manually. But in the case of vector, this is not necessary. Reallocation is possible in vector, whereas it is not possible in the dynamically allocated array.
4. Passing String Array in a function
String Array can be passed to a function similar to How we pass an Array.
Code:
#include <iostream>
#include<string>
using namespace std;
void display(string s[5]){
cout << s[2] ;
}
int main() {
string str[5] = {"Good", "Bad", "Positive", "Negative"};
display(str);
}
Output:
Here, we are passing the string array str as a parameter to a function “display”, which prints the 3rd element of the string array (“Positive”).
5. Coping from String Array to another
To copy from a String Array to another, We should copy each element individually, but the whole Array cannot be copied at one shot.
Code:
int main() {
string str[4] = {"Good", "Bad", "Positive", "Negative"};
string s[4];
// s = str; -à This line gives error as the whole array cannot be copied at a single go
// It can be copied as shown below
for( int i=0; i<4; i++) {
s[i] = str[i];
}
for(int i=0; i<4; i++) {
cout << "The element " << i+1 << " of copied array = " << s[i] << " is same as
the corresponding element in main array which is " << str[i] << endl;
}
}
Error Output:
Output:
Here when we tried to copy the whole main string array (str) to another string array (s), we got an error (as shown in screenshot1), whereas when we copied it element by element, str was copied to s, and we could verify this by seeing the second output screenshot.
Conclusion
Thus, the string is a data type that is an array of characters, and it is present only in C++. In C, we used to declare as a character array. The array of strings is an array made up of many strings. When declared statically or dynamically, it is of fixed size, and when declared in the form of a vector, size is not fixed. Each element and the character in a specific element can be easily accessed using indexing of string array.
Recommended Articles
This is a guide to String Array in C++. Here we discuss how to excess the element in String Array in C++ and allocate String Array in C++. You can also go through our other related articles to learn more-