Arrays of Strings in Multidimensional Arrays

Introduction

As we know, a string is stored in C in the form of an array. A single-dimensional character array is horizontally organized and has only one row and multiple columns corresponding to the number of characters in the string, plus an extra column for the null character. To store more than one string in an array, we need to define a two-dimensional array, where each row would contain one string. The declaration of an array that contains strings is similar to any other array declaration of two dimensions. The type specifier in this class is char.

For Example

Consider an array of strings and ask the user to enter strings and print them.

#include <stdio.h>
#include <conio.h>

void main() {
    char name_array[5][20]; // Array of 5 rows and 20 columns
    int i;
    
    clrscr();
    printf("\nProgram to accept strings and display them\n\n");
    
    for(i = 0; i < 5; i++) {
        printf("\nEnter the %2d name: ", i + 1);
        gets(name_array[i]);
        fflush(stdin);
    } // End of for loop to accept array of strings
    
    // Printing the arrays of strings
    printf("\nFollowing are the strings stored in the array:\n\n");
    for(i = 0; i < 5; i++) {
        puts(name_array[i]);
    }
    
    getch();
}

Output

Output

Initialization of Array of Strings

An array of strings can be initialized at the time of declaration or during program execution. In the above program, how can we initialize or assign values to the different rows of the string array using a program code? To initialize an array of strings at the time of declaration, the values of the strings are assigned to the different rows of the array.

char city[5][15] = {
    "New Delhi",
    "Mumbai",
    "Lucknow",
    "Jaipur",
    "Ghaziabad"
};

This declaration will create a two-dimensional array as shown below.

Two-dimensional array

Sorting Array of Strings

After seeing how an array of strings is made to generate a list or table of strings, we can sort the array of strings with in-built functions in C. It is also possible for us to sort an array of strings without using the built-in string functions. This is done by comparing each and every element of one row with the corresponding element of the next row. This method is tedious and complex. We will limit ourselves to using in-built functions to sort an array of strings as the method is simple and easy to understand.

For example, a program to sort an array of strings using in-built functions.

Source Code

#include <stdio.h>
#include <conio.h>

void main() {
    int arr[5], i, j, temp;
    clrscr();
    
    // Taking input from the user
    printf("Enter 5 elements: ");
    for(i = 0; i < 5; i++) {
        scanf("%d", &arr[i]);
    }
    
    // Sorting the array in ascending order
    for(i = 0; i < 5; i++) {
        for(j = i + 1; j < 5; j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    
    // Printing the sorted array
    printf("Sorted array: ");
    for(i = 0; i < 5; i++) {
        printf("%d ", arr[i]);	
    }
    printf("\n");
    
    getch();
}

Summary

An array of strings in C is a type of multidimensional array where each element is a string stored as a character array. This is typically defined as a two-dimensional array, where each row represents a string, and each column represents the characters in that string, such as char strings[5][20], which can store 5 strings of up to 19 characters each (plus the null terminator).

String input and output are commonly handled using functions like scanf(), fgets(), and puts(), while string manipulation is performed with functions from the <string.h> library, such as strcpy() for copying strings, strcmp() for comparing strings, and strlen() for determining the length of strings. Operations such as sorting and searching within an array of strings are implemented using nested loops, leveraging these string functions. Arrays of strings are highly useful for handling collections of text data in various applications, from simple tasks like sorting names to more complex data management systems, due to their ability to store and manipulate multiple strings efficiently within a structured format.


Similar Articles