Codeforcoding

C++ program to read and display characters of an array

C++ program to read and display characters of an array

In this tutorial, we will discuss the concept of C++ program to read and display characters of an array

In this topic, we are going to learn how to read and print characters using  one dimensional array in C++ programming language  using loops.

 

In this blog, We have already discuss that “What is an array“, type of arrays and how to access it(one dim, two dim and three dim arrays)

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 using the indices(indexes)

C++ program to read and display characters in an array
Program to read and display characters

Here,

Code to read and print characters of one dim array

Read and print characters Using for loop

In this program, we are briefing input characters of an array and then print them using for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,len;
    //integer variable declaration
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    char char_array[len];
//Array declaration
      cout<<"\n";//move to next line

       cout<<"Enter the characters for array :";
       //Ask input for elements(numbers)
    for(i=0; i<len; i++){//input using for loop
      cin>>char_array[i];
      //Reading the array elements from user
    }
    cout<<"\ndisplay the characters\n";
      for(i=0; i<len; i++){//display elements using for loop
      cout<<char_array[i];
      cout<<("\n");
    }
getch();
    return 0;
}



When the above code is executed, it produces the following result

Enter the array length: 7
Enter the characters for array: Program
Display the characters

P
r
o
g
r
a
m


Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,len;
    //integer variable declaration
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    char char_array[len];
//Array declaration
      cout<<"\n";//move to next line

      cout<<"Enter the characters for array :\n";
       //Ask input for elements(numbers)
    for(i=0; i<len; i++){//input using for loop
    cout<<"char_array["<<i<<"]:";
      cin>>char_array[i];
      //Reading the array elements from user
    }
    cout<<"\ndisplay the characters\n";
      for(i=0; i<len; i++){//display elements using for loop
      cout<<"char_array["<<i<<"]:"<<char_array[i];
      cout<<("\n");
    }
getch();
    return 0;
}




When the above code is executed, it produces the following result

Output

 

Read and display characters  using while loop

In this program, we are briefing input characters of an array and then print them using while loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,len;
    //integer variable declaration
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    char char_array[len];
//character Array declaration
      cout<<"\n";//move to next line

       cout<<"Enter the characters for array :";
       //Ask input for elements(numbers)
       i=0;
    while(i<len){//input using for loop
      cin>>char_array[i];
      //Reading the array elements from user
       i++;
    }
    cout<<"\ndisplay the characters\n";
    i=0;
      while(i<len){//display elements using for loop
      cout<<char_array[i];
      cout<<("\n");
       i++;
    }
getch();
    return 0;
}




When the above code is executed, it produces the following result

Enter the array length: 11
Enter the characters for array: Code4coding
Display the characters

C
o
d
e
4
c
o
d
i
n
g

 

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,len;
    //integer variable declaration
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    char char_array[len];
//Array declaration
      cout<<"\n";//move to next line

      cout<<"Enter the characters for array :\n";
       //Ask input for elements(numbers)
       i=0;
    while(i<len){//input using for loop
    cout<<"char_array["<<i<<"]:";
      cin>>char_array[i];
      //Reading the array elements from user
       i++;
    }
    cout<<"\ndisplay the characters\n";
    i=0;
      while(i<len){//display elements using for loop
      cout<<"char_array["<<i<<"]:"<<char_array[i];
      cout<<("\n");
       i++;
    }
getch();
    return 0;
}





When the above code is executed, it produces the following result

Output

 

Read and display characters using do- while loop

In this program, we are briefing input characters of an array and then print them using do-while loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,len;
    //integer variable declaration
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    char char_array[len];
//character Array declaration
      cout<<"\n";//move to next line

       cout<<"Enter the characters for array :";
       //Ask input for elements(numbers)
       i=0;
    do{//input using for loop
      cin>>char_array[i];
      //Reading the array elements from user
       i++;
    }while(i<len);
    cout<<"\ndisplay the characters\n";
    i=0;
     do{//display elements using for loop
      cout<<char_array[i];
      cout<<("\n");
       i++;
    } while(i<len);
getch();
    return 0;
}





When the above code is executed, it produces the following result

Enter the array length: 9
Enter the characters for array: Clanguage
Display the characters

C
l
a
n
g
u
a
g
e

 

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,len;
    //integer variable declaration
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    char char_array[len];
//Array declaration
      cout<<"\n";//move to next line

      cout<<"Enter the characters for array :\n";
       //Ask input for elements(numbers)
       i=0;
    do{//input using for loop
    cout<<"char_array["<<i<<"]:";
      cin>>char_array[i];
      //Reading the array elements from user
       i++;
    }while(i<len);
    cout<<"\ndisplay the characters\n";
    i=0;
      do{//display elements using for loop
      cout<<"char_array["<<i<<"]:"<<char_array[i];
      cout<<("\n");
       i++;
    }while(i<len);
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

Variable in C++ language

Operator in C++ language

 

Read and print string of one dim array in C
Read and print Characters of one dim array in Java language
Exit mobile version