C Programs Tutorials | IT Developer
IT Developer

C Programming - C Array of Strings



Share with a Friend

C Programming - C Array of Strings

C Array of Strings

An array of strings in C is essentially a two-dimensional array where each row is a string (an array of characters). It is a common way to store multiple strings in a single variable, making it easier to manage and manipulate a group of strings.

  1. Declaration and Initialization

Declaration:

To declare an array of strings:

C

char array_of_strings[row][column];

  • row: Number of strings.
  • column: Maximum length of each string (including the null character).

Initialization:

(a) Static Initialization:

C

char names[3][10] = {"Alice", "Bob", "Charlie"};

(b) Dynamic Initialization:

C

char names[3][10];

strcpy(names[0], "Alice");

strcpy(names[1], "Bob");

strcpy(names[2], "Charlie");

  1. Accessing and Modifying Strings

Each string can be accessed using its index:

C

printf("%s\n", names[0]); // Prints "Alice"

names[1][0] = 'R';        // Modifies "Bob" to "Rob"

  1. Input and Output

To take input for an array of strings:

C

#include <stdio.h>

int main() {

    char cities[3][20];

    printf("Enter names of 3 cities:\n");

    for (int i = 0; i < 3; i++) {

        scanf("%s", cities[i]); // Input for each string

    }

    printf("The cities are:\n");

    for (int i = 0; i < 3; i++) {

        printf("%s\n", cities[i]);

    }

    return 0;

}

Output:

Enter names of 3 cities:

London

Paris

Tokyo

The cities are:

London

Paris

Tokyo

  1. Passing Array of Strings to a Function

An array of strings can be passed to a function just like any other array.

Example:

C

#include <stdio.h>

void printStrings(char strings[][20], int n) {

    for (int i = 0; i < n; i++) {

        printf("%s\n", strings[i]);

    }

}

int main() {

    char fruits[3][20] = {"Apple", "Banana", "Cherry"};

    printStrings(fruits, 3);

    return 0;

}

  1. Using Pointers for Array of Strings

Instead of a two-dimensional array, an array of pointers to strings can be used:

C

char *names[] = {"Alice", "Bob", "Charlie"};

Advantages:

  • Saves memory since strings of different lengths don't occupy the same fixed space.
  • Easier to manage dynamically allocated strings.

Example:

C

#include <stdio.h>

int main() {

    char *languages[] = {"C", "Python", "Java"};

    for (int i = 0; i < 3; i++) {

        printf("%s\n", languages[i]);

    }

    return 0;

}

  1. Example Program: Sorting an Array of Strings

C

#include <stdio.h>

#include <string.h>

int main() {

    char strings[5][20] = {"Banana", "Apple", "Cherry", "Mango", "Grape"};

    char temp[20];

    // Sorting strings

    for (int i = 0; i < 5; i++) {

        for (int j = i + 1; j < 5; j++) {

            if (strcmp(strings[i], strings[j]) > 0) { // Compare strings

                strcpy(temp, strings[i]);

                strcpy(strings[i], strings[j]);

                strcpy(strings[j], temp);

            }

        }

    }

    printf("Sorted Strings:\n");

    for (int i = 0; i < 5; i++) {

        printf("%s\n", strings[i]);

    }

    return 0;

}

Output:

Sorted Strings:

Apple

Banana

Cherry

Grape

Mango

  1. Advantages and Limitations

Advantages:

  • Simplifies handling multiple strings.
  • Allows grouping of related strings (e.g., names, items, etc.).

Limitations:

  • Fixed size if declared as a 2D array.
  • Managing dynamic memory requires extra care with pointers to avoid memory leaks.
  1. Summary

An array of strings is a fundamental concept in C programming that provides an efficient way to handle multiple strings together. Depending on the use case, you can choose between a fixed-size 2D array or a dynamically allocated array of pointers to strings.