C++

Array of Strings C++

C++ programming language contains many data types like float, integer, strings, etc.  Talking about a string data type, this data type is an array of characters that ends with a terminating character ‘\0’. There are mostly two types of strings that are used in C++. One is the type of string that is an object of a string class. And another one is known as a C-style string. An array of a string is declared in several ways. These different ways are explained in this tutorial with examples.

String array

An array of a string is a simple array containing multiple strings.

string Fruits[4] = {"Mango", "Orange", "peach", "pineapple"};

To print the values of array of string, we use a FOR loop to display all the values.

To understand the concept of an array of strings, a user should have a command on Ubuntu operating system. Also, a user should have some basic knowledge of C++ arrays. We need two tools for the implementation of programs in C++. One is a text editor, and the other is a Ubuntu terminal used to display the resultant values.

Access elements from the string array

The array of string acts like a 2-dimensional array. The first index of a string array represents the first word/value, just like that is present in an array of data types like integer, float, etc. As we have said that an array of strings acts as a two-dimensional array. The first dimension represents the index value of the string in an array. And the second dimension represents the alphabet in the string/word. We can explain it with the example we have discussed above. Consider the same array of the string of fruits.

Fruits [2][3] = "n"

According to the above statement, the 2nd word is “orange” in the array of strings. In that string, [3] represents the 3rd alphabet, so in the string ‘orange’ it is ‘n’. By using the following method, we can easily access any character/ letter in the array of strings.

Now we will explain some approaches to declare an array of strings in the C++ language.

Example 1

In this example, we have explained the array of string declaration by using pointers. The way of initializing a pointer array is to use a “*” sign with the name of the array you want to make a pointer. Here a ‘constant’ keyword is used. This means that these arrays of strings are constant, once created, cannot be changed later in the program. The reason is that this type of string lies in the read-only area of memory.

The first step is to initialize the pointer array with the size of strings you want to add to it; as in this example, we have used 5 color names as an array of strings. Just like a simple array, we use for loop to exhibit the content of the array. This is also done by using an std:: cout statement. As we have not used the std feature in the header field as a library.

std::cout< colour[i] << "\n";

Save the code of text editor in the file with the extension of ‘.c’. Now to see the output of the code, we need to have a g++ compiler that compiles the C++ source code file and then execute it on the Ubuntu terminal. ‘-o’ is used to save the output of a file in another file. ‘string. c’ is the name of the file.

$ g++ -o string string.c
$ ./string

Example 2

A class string is used in this example. This class of string belongs to the string library. But unlike the previous example, the size of the array is not fixed. And this size of the array and the string as well can be changed later in the program. This method is only supported by C++ language but not by C. As C language does not have classes in it.

A string class library is used in the start as a header file, as we have to use string class directly in the code.

#include <string>
std:: string colour [5] = { '…'};

After that, a loop is used again to display the elements of the array.

Go to the terminal, and then execute the code. All the elements will be displayed particularly.

Example 3

The third example leads to the use of a vector string. A vector class is used to allocate an array of strings dynamically. It can also vary in size. This approach is also for the C++ language only. Like the string library, a vector library is also used in the header because we will use a vector class and a string class in the program.

#include <vector>
#include <string>

Values of the array of strings are added in the same way as we declare these values in a simple string. Both classes, string, and vector are used together in a statement.

std::vector <std :: string> colour ("…")

As the size of the array is not defined and fixed, so it can be changed at any moment. The name dynamic refers to an assignment at run time, so we can add or remove any string at any time. Similarly, we have added the color “yellow” dynamically by using a push_back function in this program.

colour.push_back("yellow");

A for loop will be used to print the values by providing the color.size() function.

Now you can see the resultant value by using the same compiler at the Ubuntu terminal. A new color is added to the array of strings.

Example 4

This C++ program is an example of passing an array of strings to a function. Starting with the main program, the first step is to declare a string object. This object will be responsible for accessing the value and passing them to the function as a parameter argument. Next, we declare a character/ array of strings. The user will enter a value. We use a getline built-in function to take the value from the user.

getline( cin, str1);

A user is asked to enter values two times; both of these are stored in the objects. One is in str, which is a character array. And the other one is stored in str1, which is a string object.

Display(str1);

Then we use two functions here; both display values. The only difference in both functions depends on the parameter values. The first function takes a character array, whereas the second function takes the string as an argument. Both these functions also use strings and character objects to accept the values.

While seeing the resultant value, you can see both the strings are displayed when we execute the code.

Conclusion

“Array of string in C++” is an article having the basic introduction to arrays and strings and how they work collectively to form an array of strings. Strings and array of strings declaration are done both statically and dynamically also. We have thrown light on both these approaches. In C++, it is quite easy to declare an array of strings, as we use string class here as compared to the primitive class C. Each example is described briefly to elaborate on the topic under discussion.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.