How to Store Array of Strings in C Programming

Introduction

In C programming, arrays are fundamental data structures used to store collections of elements of the same type. When it comes to handling text or multiple strings, arrays of characters (strings) become very useful. This tutorial will guide you through the process of storing and working with an array of strings in C.

Prerequisites:

Before diving into this tutorial, ensure you have a basic understanding of:

  • Variables and data types in C
  • Input and output operations (printf, scanf, fgets)
  • Basic control structures (for loop)

Step-by-Step Guide:

1. Setting Up the Program:

Let’s start by defining our program. We will create an array capable of holding multiple strings and then input, manipulate, and display these strings.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 5  // Maximum number of strings in the array
#define MAX_LENGTH 50  // Maximum length of each string

int main() {
    char strings[MAX_SIZE][MAX_LENGTH];  // Array of strings
    int i;

    // Input strings into the array
    printf("Enter %d strings (each less than %d characters):\n", MAX_SIZE, MAX_LENGTH);
    for (i = 0; i < MAX_SIZE; i++) {
        printf("String %d: ", i + 1);
        fgets(strings[i], MAX_LENGTH, stdin);

        // Remove newline character from fgets input
        strings[i][strcspn(strings[i], "\n")] = '\0';
    }

    // Displaying strings stored in the array
    printf("\nStrings stored in the array:\n");
    for (i = 0; i < MAX_SIZE; i++) {
        printf("String %d: %s\n", i + 1, strings[i]);
    }

    return 0;
}

2. Explanation:

– Header Files:

  • #include <stdio.h>: Provides standard input/output functions like printf and fgets.
  • #include <stdlib.h>: Includes functions related to memory allocation (not extensively used in this example).
  • #include <string.h>: Includes string manipulation functions like strlen and strcpy.

– Constants:

  • #define MAX_SIZE 5: Defines the maximum number of strings that can be stored in the array.
  • #define MAX_LENGTH 50: Defines the maximum length of each string (including the null terminator \0).

– Main Function:

  • char strings[MAX_SIZE][MAX_LENGTH];: Declares a 2-dimensional array strings capable of holding MAX_SIZE strings, each of maximum length MAX_LENGTH.

– Inputting Strings:

  • A for loop iterates through each index of the strings array.
  • fgets(strings[i], MAX_LENGTH, stdin);: Reads a string from standard input (keyboard) and stores it in strings[i].
  • strings[i][strcspn(strings[i], "\n")] = '\0';: Removes the newline character (\n) from the input string obtained by fgets(), ensuring proper string termination.

– Displaying Strings:

  • Another for loop is used to print each string stored in the strings array.

3. Running the Program:

  • Compile and Execute:
  • Save the above code into a file named array_of_strings.c.
  • Open a terminal (command prompt) and navigate to the directory containing array_of_strings.c.
  • Compile the program using gcc array_of_strings.c -o array_of_strings (assuming you have gcc installed).
  • Run the executable with ./array_of_strings (on Windows, use array_of_strings.exe instead).

4. Conclusion:

Congratulations! You’ve learned how to create and manipulate an array of strings in C. This foundational skill is crucial for handling text data efficiently in programming. Experiment with different values for MAX_SIZE and MAX_LENGTH to understand their impact on the program’s behavior.

Feel free to leave any questions or feedback in the comments section below. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top