Array

C++ exercise to print Strings of an array

C++ exercise to print Strings of an array

In this tutorial, we will discuss the concept of C++ exercise to print Strings of an array

In this topic, we are going to learn how to print  array of Strings in C++ programming(C++ exercise) 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)

Strings of an array

Here,

  • s1,s2,s3,s4 and s5 represent the string elements of the array
  • 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

Display string array elements in C++

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

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

Program 1

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

int main()
{
    string str[20];
    //Single one D array declaration
   str[0]="Mehan";
   str[1]="Slogan";
   str[2]="Nelan";
   str[3]="Ruban";
   str[4]="Alakan";

    cout<<"\nGiven students names are:\n";
    for(int i=0;  i<5; i++){
            cout<<"Your given string is:"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name
    }
    getch();
    return 0;
}

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

Output

 

Program to Display  strings of an array in C++ using for loop – #2

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

Program 1

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

int main()
{
    string str[20]={"Kani","Rubi","Ramanam","Karan","Suthan"};
    //Single one D array declaration

    cout<<"\nGiven students names are:\n";
    for(int i=0;  i<5; i++){
            cout<<"Your given string is:"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name
    }
    getch();
    return 0;
}

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

Output

 

 

Program to Display  strings of an array in C++ using while loop – #1

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

Program 1

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

int main()
{
    string str[20];
    //Single one D array declaration
   str[0]="Robert";
   str[1]="Charls";
   str[2]="Obama";
   str[3]="Joe";
   str[4]="Drump";

    cout<<"\nGiven students names are:\n";
    int i=0;
    while( i<5){
            cout<<"Your given string is:"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name
        i++;
    }
    getch();
    return 0;
}


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

Output

 

 

Program to Display  strings of an array in C++ using while loop – #2

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

Program 1

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

int main()
{

   string str[20]={"Kunal","sana","Ramaa","Karaa","Sutha"};
    //Single one D array declaration and initialization

    cout<<"\nGiven students names are:\n";
    int i=0;
    while( i<5){
            cout<<"Your given string is:"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name
        i++;
    }
    getch();
    return 0;
}

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

Your given string is:str[0]=Kunal

Your given string is:str[1]=Sona

Your given string is:str[2]=Ramma

Your given string is:str[3]=Karra

Your given string is:str[0]=Sutha

 

 

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

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

Program 1

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

int main()
{
    string str[20];
    //Single one D array declaration
   str[0]="Mugunth";
   str[1]="Naudu";
   str[2]="reddy";
   str[3]="Kurus";
   str[4]="Lamberd";

    cout<<"\nGiven students names are:\n";
    int i=0;
    do{
            cout<<"Your given string is:"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name
        i++;
    }while( i<5);
    getch();
    return 0;
}

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

Output

 

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

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

Program 2

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

int main()
{
    string str[20]={"Raja","Pooja","Guja","Sona","Suma"};
    //Single one D array declaration and initializatio

    cout<<"\nGiven students names are:\n";
    int i=0;
    do{
            cout<<"Your given string is:"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name
        i++;
    }while( i<5);
    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

Loops in C++ language

Data type in C++ language

Variable in C++ language

Operator in C++ language

 

C++ example to print elements of an array
Take Array input in Java language
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