Home / C++ / Array of Strings in C++ with examples

Array of Strings in C++ with examples

What are Strings:-

The collection of the alphabets is called string.

For Example:-

1. “London”.

2. “Austria”.

3. “Football”.

4. “Stadium “.

Array of strings:-

It contains a complete string at each index. In case of a Two-Dimensional array each complete string is stored in a unique row.

Types of Array of strings:-

There are two ways to use it:

1. By using a keyword string.

2. By using a Two-Dimensional array.

By using a keyword string:-

It can be created by declaring an simple array by using a string keyword. This method is useful if there is no need to shuffle, compare or randomize the characters of a string.

For example:

String Country [5];

By typing this syntax the compiler will generate an array of strings that would be created in a memory in which we could store names of five countries.

Input and Output Method:-

It’s Input and output method is same as the method of simple character variable.

By using a Two-Dimensional array:-

The arrays of string can be created by declaring an array by using a method of a Two-Dimensional Character array. This method is useful if we have to perform operations on the characters like shuffling, compare or randomize them.

Syntax:-

(Char keyword) (Name) (Rows) (Columns);

For example:

String Country [5] [20];

By typing this syntax the compiler will generate an array of string which has five rows and 20 columns. Each row is an indicator of a unique string and each column shows its length.

Initialization:-

Following is the method to initialize the array of strings.

Char alpha [4] [5] = {‘Q’, ‘S’, ‘A’, ‘D’,

‘B’, ‘O’, ‘X’, ‘R’,

‘U’, ‘E‘, ‘C‘, ‘I‘,

‘T’, ‘Z‘, ‘J‘, ‘H‘,

}

Char persons [4] [25] = {“Alexander”,

“Heracles”,

“Hyphistan”,

“Porus”}

Input Method:-

Two dimensional Arrays uses a nested loop for the operations on the alphabets of a string but a single loop could be used to take input by the user by just specifying the row.

For example:-

For (int u=0; u<4, u++)
{
Cout<< "Enter the Name: ";
Cin>> persons[u];
};

Output Method:-

2-D array uses a nested loop for the operations on the alphabets of a string but a single loop could be used to display the output on the screen by just specifying the row.

For example:-

For (int u=0; u<4, u++)
{
Cout<<" Your Entered name is : ";
Cout>> persons[u];
};

Operations on an Array of Strings or Two-Dimensional Character Array:-

C++ array of strings
Array of Strings in C++

Following example will illustrate how to perform operations on a Character array.

The following Puzzle contain the names of the companions of a Frodo begins.

Q W E R T Y Y U I O

A S D F G R H J A P

Z R S X R P C V B N

W Q A E G I M L Y M

E R M G T P Y E U I

A S W D O I F G H P

K L I Z X N C O V B

N M S Q W E R L T Y

U I E O P A S A D F

F L A D N A G S G H

From this puzzle we have to find LEGOLAS and the desired output on the screen should be like that:

+ + + + + + + + + +

+ + + + + + + + + +

+ + + + + + + L + +

+ + + + + + + E + +

+ + + + + + + G + +

+ + + + + + + O + +

+ + + + + + + L + +

+ + + + + + + A + +

+ + + + + + + S + +

Following solution of a puzzle would completely Explains the operations on a two-Dimensional array.

#include<iostream>
#include<conio.h>
#include<string.h>
#include<cstring>
using namespace std;
main()
{
char box[10][10]= { 'Q','W','E','R','T','Y','Y','U','I','O',
'A','S','D','F','G','R','H','J','A','P',
'Z','R','S','X','R','P','C','V','B','N',
'W','Q','A','E','G','I','M','L','Y','M',
'E','R','M','G','T','P','Y','E','U','I',
'A','S','W','D','O','I','F','G','H','P',
'K','L','I','Z','X','N','C','O','V','B',
'N','M','S','Q','W','E','R','L','T','Y',
'U','I','E','O','P','A','S','A','D','F',
'F','L','A','D','N','A','G','S','G','H',
};
// If you have to perform different operations on a string then I will recommend to use this way of initialization because by using this way it will be easy to perform operations on the alphabets of a string. //
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if (box[i-1][j]== 'V' && box[i+1][j]== 'E' )
{
cout<<box[i][j];
}
else if(box[i][j]== 'E' && box[i][j+1]== 'U' )
{
cout<<box[i][j];
}
else if(box[i][j]== 'G' && box[i+1][j]== 'O' )
{
cout<<box[i][j];
}
else if(box[i][j]== 'O' && box[i+1][j]== 'L' )
{
cout<<box[i][j];
}
else if(box[i][j]== 'L' && box[i+1][j]== 'A' )
{
cout<<box[i][j];
}
else if(box[i][j]== 'A' && box[i+1][j]== 'S' )
{
cout<<box[i][j];
}
else if(box[i][j]== 'S' && box[i][j+1]== 'G' )
{
cout<<box[i][j];
}
else
{
cout<<"+";
}
}
cout<<endl;
}
getch();
}

Explanation:-

[yop_poll id=”2″]

The above program will initialize the puzzle in a 2-D array. Then two For loops are used to perform the operations on the characters of a string. So, we can perform different logics to print the names of the companions of a Frodo begins. Here I made a code which will show name of LEGOLAS as an Output. The logic used above is the code which will check the alphabets above and below the required alphabet, we already know about these alphabets because they are shown in the puzzle. If the required alphabets at the given conditions are matched then it will print the required alphabet and so on.

For example:-

As you can see L is the required alphabet, in the same column on the above there is E and below is V. Therefore the given condition is satisfied and the program will print L and like this all the other alphabets, those indexes where condition does not satisfied it will print + sign.

If you like my article on array of strings and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

About

(royal52) is a software engineer. He is good at authoritative link building, Content Writing, SEO, Web Development, Wordpress, HTML, CSS, JAVASCRIPT, Game Development, Unity3d, Graphic Designing, Logo Designing, Android Development and Ios Development. Last but not least, he is passionate about development and gaming.

Check Also

File Handling in C++ with Examples

Files are used to save or retrieve data from storage devices, e.g. we can store …

Leave a Reply