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
Two dimension array in C language
For loop in C programming language
While loop in C programming language
Do-while loop in C programming language
Code to create and print array of string
Read and print array of string Using for loop
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 <stdio.h> #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<n; i++){//for loop for input String printf("String[%d]: ",i+1); getchar(); scanf("%[^\n]s",strings[i]); } printf("\nEntered strings are:\n"); for(i=0; i<n; i++){//for loop for output entered String printf("[%2d]: %s\n",i+1,strings[i]); } getch(); return 0; }
When the above code is executed, it produces the following result
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 <stdio.h> #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<n){ printf("String[%d]: ",i+1); getchar(); scanf("%[^\n]s",strings[i]); i++; } printf("\nEntered strings are:\n"); i=0; while(i<n){ printf("[%2d]: %s\n",i+1,strings[i]); i++; } getch(); return 0; }
When the above code is executed, it produces the following result
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 <stdio.h> #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<n); printf("\nEntered strings are:\n"); i=0; do{ printf("[%2d]: %s\n",i+1,strings[i]); i++; } while(i<n); getch(); return 0; }
When the above code is executed, it produces the following result
Similar post
Code to read and print integers of an array in Java
Code to read and print strings of an array in Java
Code to read and print characters of an array in Java
Code to read and print integers of an array in C++
Code to read and print strings of an array in C++
Code to read and print characters of an array in C++
Code to read and print integers of an array in C
Code to read and print strings of an array in C
Suggested for you
One dimensional array in C language
One dimensional array in C++ language
Two dimension array in Java language
Two dimension array in C language
Two dimension array in C++ language
Three dimension array in Java language
Three dimension array in C language
Three dimension array in C++ language