• C Programming Video Tutorials

Array of Strings in C



In C language, a string is a one-dimensional array of char data type. It is a sequence of characters terminated by null character (\0). An array of strings therefore is an array of char arrays.

To declare a string, we use the statement −

char string[] = {'H', 'e', 'l', 'l', 'o', '\0'};
Or
char string = "Hello";

Declare and Initialize an Array of Strings

To construct an array of strings, the following syntax is used −

char strings [no of strings] [max size of each string];

Let us declare and initialize an array of strings to store the names of 10 computer languages, each with the maximum length of 15 characters.

char langs [10][15] = {
   "PYTHON", "JAVASCRIPT", "PHP",
   "NODE JS", "HTML", "KOTLIN", "C++",
   "REACT JS", "RUST", "VBSCRIPT"
};

How is this array stored in the memory? We know that each char type occupies 1 byte in the memory. Hence, this array will be allocated a block of 150 bytes. Although this block is contagious memory locations, each group of 15 bytes constitutes a row.

Assuming that the array is located at the memory address 1000, the logical layout of this array can be shown as in the following figure −

Situated Memory Address

Example

We can use a loop to print the contents of this string array −

#include <stdio.h>

int main (){

   char langs [10][15] = {
      "PYTHON", "JAVASCRIPT", "PHP",
      "NODE JS", "HTML", "KOTLIN", "C++",
      "REACT JS", "RUST", "VBSCRIPT"
   };

   for (int i = 0; i < 10; i++){
      printf("%s\n", langs[i]);
   }
   
   return 0;
}

Output

When you run this code, it will produce the following output −

PYTHON
JAVASCRIPT
PHP
NODE JS
HTML
KOTLIN
C++
REACT JS
RUST
VBSCRIPT

Note: The size of each string is not equal to the row size in the declaration of the array. The "\0" symbol signals the termination of the string and the remaining cells in the row are empty. Thus, a substantial part of the memory allocated to the array is unused and thus wasted.

Using Pointers for Better Memory Management

To use the memory more efficiently, we can use the pointers. Instead of a 2D char array, we declare a 1D array of "char *" type.

 
char *langs[10] = {
   "PYTHON", "JAVASCRIPT", "PHP",
   "NODE JS", "HTML", "KOTLIN", "C++", 
   "REACT JS", "RUST", "VBSCRIPT"
};

In the 2D array of characters, the strings occupied 150 bytes. As against this, in an array of pointers, the strings occupy far less number of bytes, as each string is randomly allocated memory as shown below −

Randomly Allocated Memory

Note: Here, lang[ ] is an array of pointers of individual strings.

Array of Pointers

Example

We can use a for loop as follows to print the array of strings −

#include <stdio.h>

int main(){

   char *langs[10] = {
      "PYTHON", "JAVASCRIPT", "PHP",
      "NODE JS", "HTML", "KOTLIN", "C++", 
      "REACT JS", "RUST", "VBSCRIPT"
   };

   for (int i = 0; i < 10; i++)
      printf("%s\n", langs[i]);

   return 0;
}

Output

When you run this code, it will produce the following output −

PYTHON
JAVASCRIPT
PHP
NODE JS
HTML
KOTLIN
C++
REACT JS
RUST
VBSCRIPT

Here, langs is a pointer to an array of 10 strings. Therefore, if langs[0] points to the address 5000, then "langs + 1" will point to the address 5004 which stores the pointer to the second string.

Hence, we can also use the following variation of the loop to print the array of strings −

for(int i = 0; i < 10; i++){
   printf("%s\n", *(langs + i));
}

When strings are stored in array, there are a lot use cases. Let study some of the use cases.

Find the String with the Largest Length

In the following example, we store the length of first string and its position (which is "0") in the variables "l" and "p" respectively. Inside the for loop, we update these variables whenever a string of larger length is found.

Example

Take a look at the following example −

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

int main (){

   char langs [10][15]  = {
      "PYTHON", "JAVASCRIPT", "PHP",
      "NODE JS", "HTML", "KOTLIN", "C++",
      "REACT JS", "RUST", "VBSCRIPT"
   };

   int l = strlen(langs[0]); 
   int p = 0;

   for (int i = 0; i < 10; i++){
      if (strlen(langs[i]) >= l){
         l = strlen(langs[i]);
         p = i;
      }
   }
   printf("Language with the longest name: %s Length: %d", langs[p], l);

   return 0;
}

Output

When you run this code, it will produce the following output −

Language with longest name: JAVASCRIPT Length: 10

Sort a String Array in Ascending Order

We need to use the strcmp() function to compare two strings. If the value of comparison of strings is greater than 0, it means the first argument string appears later than the second in alphabetical order. We then swap these two strings using the strcmp() function.

Example

Take a look at the following example −

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

int main (){

   char langs [10][15] = {
      "PYTHON", "JAVASCRIPT", "PHP",
      "NODE JS", "HTML", "KOTLIN", "C++",
      "REACT JS", "RUST", "VBSCRIPT"
   };

   int i, j;
   char temp[15];
   for (i = 0; i < 9; i++){
      for (j = i + 1; j < 10; j++){
         if (strcmp(langs[i], langs[j]) > 0){
            strcpy(temp, langs[i]);
            strcpy(langs[i], langs[j]);
            strcpy(langs[j], temp);
         }
      }
   }

   for (i = 0; i < 10; i++){
      printf("%s\n", langs[i]);
   }

   return 0;
}

Output

When you run this code, it will produce the following output −

C++
HTML
JAVASCRIPT
KOTLIN
NODE JS
PHP
PYTHON
REACT JS
RUST
VBSCRIPT

In this chapter, we explained how you can declare an array of strings and how you can manipulate it with the help of string functions.

Advertisements