30% Therapy – 40% Practice – 30% Work project

Array of Strings in C



In C programming language, a is an array of character sequences terminated by NULL, it is a one-dimensional array of characters. And, the array of strings is an array of strings (character array).

What is an Array of Strings in C?

Thus, an array of strings can be defined as –

An array of strings is a two-dimensional array of character-type arrays where each character array (string) is null-terminated.

To declare a string, we use the statement −

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

Declare and Initialize an Array of Strings

To declare an array of strings, you need to declare a of character types, where the first subscript is the total number of strings and the second subscript is the maximum size of each string.

To initialize an array of strings, you need to provide the multiple strings inside the double quotes separated by the commas.

Syntax

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

char strings [no_of_strings] [max_size_of_each_string];

Example

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"
};

Printing An Array of Strings

A string can be printed using the with %s format specifier. To print each string of an array of strings, you can use the till the number of strings.

Example

In the following example, we are declaring, initializing, and printing an array of string −

#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("%sn", 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 “” 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.

How an Array of Strings is Stored in 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

An Array of Strings with Pointers

To use the memory more efficiently, we can use the . 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 , 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("%sn", 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("%sn", *(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 “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 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("%sn", 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 .

Translate »