Array of Strings in C

In this class, we will try to understand the Array of Strings in C.

Array of Strings in C

We have already covered the concepts on strings in our previous classes.

Array of Strings

In C, we have a provision to create an array of strings.

So far, in our previous classes, we have seen how to create a string (an array of characters).

An array of strings is an array with more than one string.

As we have already that we can create an array of arrays by declaring a two-dimensional array.

In the same way, we can create an array of strings by using a two-dimensional array.

Array of Strings Example 1

The image is the program to understand the concept of an array of strings.

Array of Strings in C Example 1
Array of Strings in C Example 1

The second line in the above code is the declaration of an array of strings.

In the above declaration, we have created an array of four strings, as shown in the image below.

Array of Strings in C Visualization
Array of Strings in C Visualization

If the initialization is done in the declaration, there is no need to declare rows, and the compiler automatically took those rows.

The program is to find the planets whose names start with ‘M’.

In our example, we have two planets, Mercury and Mars.

We have used the rows and columns to access a specific character in the string in the if condition.

If the character is equal to ‘M,’ then we will print the entire string.

To access the entire string, we have to use only the row number.

To access a character in the string, we have to use both row and column numbers.

Array of Strings Example 2

In the above example, we have initialized the array, but we will allow the user to enter the details in this example.

The image below is the program to understand.

Array of Strings in C Example 2
Array of Strings in C Example 2

The first for loop in the above example is to allow the user to give the string inputs.

The remaining part of the program is the same as the above example.