Array of String
Array of String
Sometimes we often use lists of character strings, such as the list of names of students in a class, a list
of names of employees in an organization, a list of places, etc. A list of names can be treated as a table
of string. To store the entire list we use a 2d array of strings in C language.
A string is a 1-D array of characters, so an array of strings is a 2-D array of characters. Just like we can
create a 2-D array of int, float etc; we can also create a 2-D array of character or array of strings. Here
is how we can declare a 2-D array of characters.
char string-array-name[row-size][column-size];
Here the first index (row-size) specifies the maximum number of strings in the array, and the second
index (column-size) specifies the maximum length of every individual string.
For example, char language[5][10];
In the “language” array we can store a maximum of 5 Strings and each String can have a maximum of
10 characters.
In C language, each character take 1 byte of memory. For the “language” array it will allocate 50 bytes
(1*5*10) of memory. Where each String will have 10 bytes (1*10) of memory space.
Two dimensional (2D) strings in C language can be directly initialized as shown below,
char language[5][10] =
{
{'J','a','v','a','\0'},
{'P','y','t','h','o','n','\0'},
{'C','+','+','\0'},
{'H','T','M','L','\0'},
{'S','Q','L','\0'}
};
Since it is a two-dimension of characters, so each String (1-D array of characters) must end
with null character i.e. ‘\0’
The second way of declaring the array of strings is a lengthy process, and other programmers
can’t read them easily compared to the previous declaration, that why most of the time we
prefer the first declaration.
The each String in this array can be accessed by using its index number. The index of array
always starts with 0.
Note1:- the number of characters (column-size) must be declared at the time of the
initialization of the two-dimensional array of strings.
// it is valid
char language[ ][10] = {"Java", "Python", "C++", "HTML", "SQL"};
// invalid
char language[ ][ ] = {"Java", "Python", "C++", "HTML", "SQL"};
// invalid
char language[5][ ] = {"Java", "Python", "C++", "HTML", "SQL"};
Note2:- Once we initialize the array of String then we can’t directly assign a new String.
char language[5][10] = {"Java", "Python", "C++", "HTML", "SQL"};
The two-dimensional array of strings can be read by using loops. To read we can use scanf(),
gets(), fgets() or any other methods to read the string.
// reading strings using for loop
for(i=0;i<n;i++)
{
scanf("%s[^\n]",name[i]);
}
Or,
// Dispaying strings
printf("Languages are:\n");
for(int i=0;i<5;i++)
puts(language[i]);
return 0;
}
Output:-
Languages are:
Java
Python
C++
HTML
SQL
// dispaying strings
printf("\nEntered names are:\n");
for(i=0;i<n;i++)
puts(name[i]);
return 0;
}
Output:-
int main()
{
int i, j;
char str[10][50], temp[50];
printf("Enter 10 words:\n");
return 0;
}
Olivia
A Output: Enter 10 words:
C++
Java
PHP
Python
Perl
Ruby
JavaScript
PHP
In lexicographical order:
C++
Java
JavaScript
PHP
PHP
Perl
Python
Ruby
va
Isabella