Program to create and print an array of string in C
Program to create and print an array of string in C
In this tutorial, we will discuss the concept of program to create and print array of string in C
In this topic, we are going to learn how to create, read and print array of string in C programming language using for loop while loop and do-while loop
We have already discuss that “What is an array“, type of arrays and how to access it
Arrays are the special type of variables to store Multiple type of values(int, string,char, etc)under the same name in the continuous memory location. we can be easily accessed through the indices(indexes)
To understand this example, you should have the knowledge of the following C programming topics
In this program, we are briefing how to input(create and read) strings of an array and then print them using for loop in C language
Program 1
#include
#define MAX_STRINGS 10
#define STRING_LENGTH 50
int main()
{
char strings[MAX_STRINGS][STRING_LENGTH];
int i,n;
printf("Enter Total Number Of String: ");
scanf("%d",&n);
printf("\nEnter strings\n");
for(i=0; i
When the above code is executed, it produces the following result
Output
Read and print array of string Using while loop
In this program, we are briefing how to input strings of an array and then print them using while loop in C language
Program 2
#include
#define MAX_STRINGS 10
#define STRING_LENGTH 50
int main()
{
char strings[MAX_STRINGS][STRING_LENGTH];
int i,n;
printf("Enter Total Number Of String: ");
scanf("%d",&n);
printf("\nEnter strings\n");
i=0;
while(i
When the above code is executed, it produces the following result
output
Read and print array of string Using do-while loop
In this program, we are briefing how to input strings of an array and then print them using do-while loop in C language
Program 3
#include
#define MAX_STRINGS 10
#define STRING_LENGTH 50
int main()
{
char strings[MAX_STRINGS][STRING_LENGTH];
int i,n;
printf("Enter Total Number Of String: ");
scanf("%d",&n);
printf("\nEnter strings\n");
i=0;
do{
printf("String[%d]: ",i+1);
getchar();
scanf("%[^\n]s",strings[i]);
i++;
} while(i
When the above code is executed, it produces the following result