How to store words in an array in C?

18 Oct, 2019
We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C.
To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index number of the words and the column number will denote the particular character in that word.
Direct initialization: In this method, the words are already known and the 2-D char array is created with these words directly.Syntax for Direct initialization:char array[][20] = {"Geek1", "Geek2", "Geek3", ..."};Syntax for accessing a word:Lets say we need to fetch the ith word:

array[i]
Below is the implementation of the above approach:// C program to store words in an array  #include <stdio.h>  int main(){    int i;      // Direct initialization of 2-D char array    char array[][20] = { "Geek1", "Geek2", "Geek3" };      // print the words    for (i = 0; i < 3; i++)        printf("%s\n", array[i]);      return 0;}Output:Geek1
Geek2
Geek3
By taking input from user: In this method, the number of words and words are given by the user and we have to create and map the 2-D char array for each word.Syntax:// Declaration of 2-D char array
// where n is the number of words
char array[n][20];

// Initialization of 2-D char array
for (i = 0; i < n; i++)
    scanf("%s", array[i]);
Syntax for accessing a word:Lets say we need to fetch the ith word:

array[i]
Below is the implementation of the above approach:// C program to store words in an array  #include <stdio.h>  int main(){    int i;      // Lets say we have 3 words    int n = 3;      // Declaration of 2-D char array    char array[n][20];      // Initialization of 2-D char array    for (i = 0; i < 3; i++)        scanf("%s", array[i]);      // print the words    for (i = 0; i < 3; i++)        printf("%s\n", array[i]);      return 0;}Input:Geek1
Geek2
Geek3
Output:Geek1
Geek2
Geek3

Direct initialization: In this method, the words are already known and the 2-D char array is created with these words directly.Syntax for Direct initialization:char array[][20] = {"Geek1", "Geek2", "Geek3", ..."};Syntax for accessing a word:Lets say we need to fetch the ith word:

array[i]
Below is the implementation of the above approach:// C program to store words in an array  #include <stdio.h>  int main(){    int i;      // Direct initialization of 2-D char array    char array[][20] = { "Geek1", "Geek2", "Geek3" };      // print the words    for (i = 0; i < 3; i++)        printf("%s\n", array[i]);      return 0;}Output:Geek1
Geek2
Geek3

Syntax for Direct initialization:
char array[][20] = {"Geek1", "Geek2", "Geek3", ..."};
Syntax for accessing a word:
Lets say we need to fetch the ith word:

array[i]

Below is the implementation of the above approach:
// C program to store words in an array  #include <stdio.h>  int main(){    int i;      // Direct initialization of 2-D char array    char array[][20] = { "Geek1", "Geek2", "Geek3" };      // print the words    for (i = 0; i < 3; i++)        printf("%s\n", array[i]);      return 0;}
Geek1
Geek2
Geek3

By taking input from user: In this method, the number of words and words are given by the user and we have to create and map the 2-D char array for each word.Syntax:// Declaration of 2-D char array
// where n is the number of words
char array[n][20];

// Initialization of 2-D char array
for (i = 0; i < n; i++)
    scanf("%s", array[i]);
Syntax for accessing a word:Lets say we need to fetch the ith word:

array[i]
Below is the implementation of the above approach:// C program to store words in an array  #include <stdio.h>  int main(){    int i;      // Lets say we have 3 words    int n = 3;      // Declaration of 2-D char array    char array[n][20];      // Initialization of 2-D char array    for (i = 0; i < 3; i++)        scanf("%s", array[i]);      // print the words    for (i = 0; i < 3; i++)        printf("%s\n", array[i]);      return 0;}Input:Geek1
Geek2
Geek3
Output:Geek1
Geek2
Geek3

Syntax:
// Declaration of 2-D char array
// where n is the number of words
char array[n][20];

// Initialization of 2-D char array
for (i = 0; i < n; i++)
    scanf("%s", array[i]);

Syntax for accessing a word:
Lets say we need to fetch the ith word:

array[i]

Below is the implementation of the above approach:
// C program to store words in an array  #include <stdio.h>  int main(){    int i;      // Lets say we have 3 words    int n = 3;      // Declaration of 2-D char array    char array[n][20];      // Initialization of 2-D char array    for (i = 0; i < 3; i++)        scanf("%s", array[i]);      // print the words    for (i = 0; i < 3; i++)        printf("%s\n", array[i]);      return 0;}
Geek1
Geek2
Geek3

Output:
Geek1
Geek2
Geek3

C-String
C Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Producer Consumer Problem in C
Difference between break and continue statement in C
Exit codes in C/C++ with Examples
C Hello World Program
Handling multiple clients on server with multithreading using Socket Programming in C/C++
C program to find the length of a string
C / C++ Program for Dijkstra's shortest path algorithm | Greedy Algo-7
C Program for Tower of Hanoi
Conditional wait and signal in multi-threading