EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

String Array in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » String Array in C++

string array in c++

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’) which denotes 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’

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 case of the string, 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"};

To print the whole string array, for loop can be used:

Code:

for(int i = 0; i<4; i++) {
cout << Animals[i] << endl;
}

Output:

output1 string array

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.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,837 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

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:

output2 string array

Here the array color 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:

output3 string array

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 color

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:

output 4 string array

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 case of dynamically allocating string array using the command “new”, we need to manually deallocate the allocated memory. 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:

output5

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:

output6

Output:

output7

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 which 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 element in String Array in C++, and allocation of String Array in C++. You can also go through our other related articles to learn more-

  1. String Array in Python
  2. StringBuffer in Java
  3. Arrays in C++
  4. String Array in C#

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More