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
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
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
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
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…