Array

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

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 <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

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 <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

output

 

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

Array in Programming language

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

for loop in C language

while loop in C language

do-while loop in C language

Operator in C language

Data type in C language

variable in C language

 

Print Character array elements in Java
C++ example to print elements of an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

22 minutes ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago