In C/C++, a string is a 1-D array of characters and an array of string in C is a 2D array of characters. This comes quite handy in C++. There are 3 ways in which an Array of Strings in C or C++ can be created.
-
Using Pointers(Both C and C++): We actually create string literals by creating an array of pointers.
// C++ program to demonstrate array of strings using// 2D character array#include <bits/stdc++.h>usingnamespacestd;intmain(){// Initialize array of pointerconstchar*colour[4] = {"Blue","Red","Orange","Yellow"};// Printing Strings stored in 2D arrayfor(inti = 0; i < 4; i++)cout << colour[i] <<"\n";return0;}chevron_rightfilter_noneOutput:Blue Red Orange Yellow
Drawbacks:
- Number of strings are fixed
- Strings are constants and contents cannot be changed.
-
Using 2D array(Both C and C++): This method is useful for shuffling, comparing and accessing characters randomly.
Syntax:
Char “Name” [“Number of Strings”][“MaxSize of String”]
Example:
Char colour [4][10] // Here 4 colours can be inserted // with max String size of 10.
// C++ program to demonstrate array of strings using// 2D character array#include <bits/stdc++.h>usingnamespacestd;intmain(){// Initialize 2D arraycharcolour[4][10] = {"Blue","Red","Orange","Yellow"};// Printing Strings stored in 2D arrayfor(inti = 0; i < 4; i++)cout << colour[i] <<"\n";return0;}chevron_rightfilter_noneOutput:Blue Red Orange Yellow
Drawbacks:
- Both the number of Strings and Size of String are fixed.
- A 2D array is allocated, whose second dimension is equal to maximum sized string which causes wastage of space.
- Using string Keyword (Only in C++): In this method, size of the string is not fixed, hence space is saved.
Syntax:String Name_ofString [number_of_string]
Example:
String geeks[50]
// C++ program to demonstrate array of strings using// array of strings.#include <bits/stdc++.h>usingnamespacestd;intmain(){// Initialize String Arraystring colour[4] = {"Blue","Red","Orange","Yellow"};// Print Stringsfor(inti = 0; i < 4; i++)cout << colour[i] <<"\n";}chevron_rightfilter_noneOutput:Blue Red Orange Yellow
Drawback: The array is of fixed size.
- Using Vectors(Only C++): STL Container Vector can be used to dynamically allocate array.
Syntax:
Vector “Name”
Example:
Vector Colour
// C++ program to demonstrate vector of strings using#include <bits/stdc++.h>usingnamespacestd;intmain(){// Declaring Vector of String typevector<string> colour;// Initialize vector with strings using push_back// commandcolour.push_back("Blue");colour.push_back("Red");colour.push_back("Orange");colour.push_back("Yellow");// Print Strings stored in Vectorfor(inti = 0; i < colour.size(); i++)cout << colour[i] <<"\n";}chevron_rightfilter_noneOutput:Blue Red Orange Yellow
Conclusion: Out of all the methods, Vector seems to be the best way for creating an array of Strings in C++.
This article is contributed by Kartik Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all Strings from array A[] having all strings from array B[] as subsequence
- Different ways for Integer to String Conversions In Java
- Different ways of Method Overloading in Java
- Different ways of Reading a text file in Java
- Different ways to delete elements in std::map (erase() and clear())
- Different ways to declare variable as constant in C and C++
- Initialize a vector in C++ (5 different ways)
- Preventing Object Copy in C++ (3 Different Ways)
- Ways to express a number as product of two different factors
- Reverse string in Python (5 different ways)
- Print system time in C++ (3 different ways)
- Different ways to initialize a variable in C/C++
- Different ways to use Const with Reference to a Pointer in C++
- 7 different ways to Add Two Numbers in C/C++
- Different Ways to Remove all the Digits from String in Java
- Search in an array of strings where non-empty strings are sorted
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Search strings with the help of given pattern in an Array of strings
- Print all strings of maximum length from an array of strings
- Minimum cost to sort strings using reversal operations of different costs
Improved By :


