String Array in C++ programming is a collection of the string data items, which is accessed by using common name. we use two types of arrays in C++ programming one-dimensional array and two-dimensional array.
#include<iostream.h>
#include<conio.h>
void main()
{
char arr[5]; //'arr' is name of Array and 5 is length of Array.
arr[0] = 'H'; //assigning Element to Every Index of 'arr' Array.
arr[1] = 'i';
arr[2] = 'L';
arr[3] = 'a';
arr[4] = 'L';
arr[5] = 's';
for(int i = 0; i < 6; i++)
{
cout<<arr[i];
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string>
void main()
{ //'arr' is name of Array which length is 4 and assigning
string arr[4] = {"HiLaLs", "Khan", "Afridi", "PK"}; //values to it.
for(int i = 0; i < 4; i++) //loop looping from 0 to 4.
{
cout<<arr[i]<<endl; //and printing 'arr' Array Elements.
}
getch(); //U also write like
} //'string arr[] = {"HiLaLs", "Khan", "Afridi", "PK"};'.
#include<iostream.h>
#include<conio.h>
void main()
{ //assigning Elements to 'arr' array.
int arr[5] = {1, 2, 4, 22, 10};
int a = arr[2]; //getting 2nd Element of 'arr' Array and assigning to 'a'.
int b = arr[4]; //getting 4th Element of 'arr' Array and assigning to 'b'.
cout<<"In 2nd Index: "<<a<<endl; //and printing.
cout<<"In 4th Index: "<<b<<endl;
getch();
}