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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago