Categories: category

Print char array in C++ language

Print char array in C++ language

In this tutorial, we will discuss the concept of Print char array in C++ language

In this topic, we are going to learn how to print  array of Characters in C++ programming language  using for, while and do-while 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)

Char array in C++

Here,

  • 0,1,2,3 and 4 represent the index of the array which is used to access the array elements
  • 1,2,3,4 and 5 represent the length of the array

Print char array

Program to Display  characters of an array in C++ using for loop -#1

In this program, we are briefing print  array of characters using for loop in C++ language

Program 1

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

int main()
{
    int i;

    char char_array[5];
//Array declaration - char array

char_array[0]='X';
char_array[1]='U';
char_array[2]='B';
char_array[3]='D';
char_array[4]='S';

    cout<<"\ndisplay the characters\n";
      for(i=0; i<5; 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

display the characters

char_array[0]='X';
char_array[1]='U';
char_array[2]='B';
char_array[3]='D';
char_array[4]='S';

 

 

Program to Display  characters of an array in C++ using for loop -#2

In this program, we are briefing print  array of characters using for loop in C++ language

Program 1

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

int main()
{
    int i;

    char char_array[]={'K','A','N','N','A','N'};
//Array declaration - char array

    cout<<"\ndisplay the characters\n";
      for(i=0; i<=5; 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

display the characters

char_array[0]=K;
char_array[1]=A;
char_array[2]=N;
char_array[3]=N;
char_array[4]=A;
char_array[4]=N;

 

 

Program to Display  characters of an array in C++ using while loop -#1

In this program, we are briefing print  array of characters using while loop in C++ language

Program 1

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

int main()
{
    int i;

    char char_array[4];
//Array declaration - char array

char_array[0]='c';
char_array[1]='h';
char_array[2]='a';
char_array[3]='r';


    cout<<"\ndisplay the characters\n";
    i=0;
      while(i<4){//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

display the characters

char_array[0]=c;
char_array[1]=h;
char_array[2]=a;
char_array[3]=r;

 

Program to Display  characters of an array in C++ using while loop -#2

In this program, we are briefing print  array of characters using while loop in C++ language

Program 1

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

int main()
{
    int i;

    char char_array[]={'M','A','R','A','N'};
//Array declaration - char array

    cout<<"\ndisplay the characters\n";
    i=0;
      while(i<5){//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

display the characters

char_array[0]=M;
char_array[1]=A;
char_array[2]=R;
char_array[3]=A;
char_array[4]=N;

 

Program to Display  characters of an array in C++ using do-while loop -#1

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

Program 1

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

int main()
{
    int i;

    char char_array[6];
//Array declaration - char array

char_array[0]='S';
char_array[1]='t';
char_array[2]='r';
char_array[3]='i';
char_array[4]='n';
char_array[5]='g';


    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<6);
getch();
    return 0;
}

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

display the characters

char_array[0]=S;
char_array[1]=t;
char_array[2]=r;
char_array[3]=i;
char_array[4]=n;
char_array[5]=g;

Program to Display  characters of an array in C++ using do-while loop -#2

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

Program 2

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

int main()
{
    int i;

    char char_array[]={'J','A','V','A'};
//Array declaration - char array

    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<4);
getch();
    return 0;
}

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

display the characters

char_array[0]=J;
char_array[1]=A;
char_array[2]=V;
char_array[3]=A;

 

 

 

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

 

   
Calculate product of two floating point numbers in Python
C++ code to convert Celsius into Fahrenheit using the function
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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 weeks ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

3 weeks ago

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

3 weeks 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…

4 weeks ago

C# inverted full pyramid star pattern

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

2 months ago

C# Full Pyramid star pattern program

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

2 months ago