Array

C++ code to Read and print string of one dim array

C++ code to Read and print string of one dim array

In this tutorial, we will discuss the concept of C++ code to Read and print string of one dim array

In this topic, we are going to learn how to read and print strings of a 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

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)

one dim array

Here,

  • S1,S2,S3,S4 and S5 represent the 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

Code to read and print elements of one dim array

Read and print strings In an array

Program 1

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

int main()
{
    char str[20];
    cout<<"Enter your name\n";
    cin>>str;
    cout<<"\nyou entered:\n"<<str<<endl;

    getch();
    return 0;
}

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

Enter your name:
Kamalakumar

You entered :
Kamalakumar

 

Program 2

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

int main()
{
    char str[20];
    cout<<"Enter your name\n";
    cin.get(str,20);
    cout<<"\nCheck your name:\n"<<str<<endl;

    getch();
    return 0;
}

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

Enter your name:
Kamalan

Check your name:
Kamalan

 

Read and print strings In an array using for loop

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

Program 3

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

int main()
{
    string str[20];
    int len;
    cout<<"\nEnter array length\n";
    cin>>len;

    cout<<"Enter students name\n";
    for(int i=0; i<len; i++){
        cin>>str[i];
    }

    cout<<"\nEntered students names are:\n";
    for(int i=0;  i<len; i++){
        cout<<str[i]<<"\n";
    }


    getch();
    return 0;
}

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

Output

 

Program 4

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

int main()
{
    string str[20];
    // one D array declaration
    int len;//Declare variable for length
    cout<<"Enter array length\n";
    //Ask input from the user for length
    cin>>len;
//Reading the input for length
    cout<<"\nEnter students name\n";
    //Ask enter the student name
    for(int i=0; i<len; i++){
        cout<<"Enter the string"<<" str=["<<i<<"]: ";
        cin>>str[i];
    }//Enter the students name using for loop

    cout<<"\nEntered students names are:\n";
    for(int i=0;  i<len; i++){
            cout<<"Your entered 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

 

Read and print strings In an array using while loop

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

Program 5

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

int main()
{
    string str[20];
    int len;
    cout<<"Enter array length\n";
    cin>>len;

    cout<<"\nEnter students name\n";
    int i=0;
    while(i<len){
        cin>>str[i];
         i++;
    }

    cout<<"\nEntered students names are:\n";
    int j=0;
    while(j<len){
        cout<<str[j]<<"\n";
         j++;
    }


    getch();
    return 0;
}


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

Output

 

Program 6

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

int main()
{
    string str[20];
    //String array declaration
    int len;
    //Variable declaration - integer
    cout<<"Enter array length\n";
    //Ask input for array length
    cin>>len;
//Reading the array length
    cout<<"\nEnter students name\n";
    int i=0;
    while(i<len){
          cout<<"Enter the string"<<" str=["<<i<<"]: ";
        cin>>str[i];
         i++;
    }//Enter the student name using while loop

    cout<<"\nEntered students names are:\n";
    int j=0;
    while(j<len){
        cout<<"Your entered string is:"<<" str=["<<j<<"]: ";
        cout<<str[j]<<"\n";
         j++;
    }// Display the student name using while loop


    getch();
    return 0;
}


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

Output

 

Read and print strings In an array using do-while loop

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

Program 7

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

int main()
{
    string str[20];
    int len;
    cout<<"Enter array length\n";
    cin>>len;

    cout<<"\nEnter students name\n";
    int i=0;
    do{
        cin>>str[i];
         i++;
    }while(i<len);

    cout<<"\nEntered students names are:\n";
    int j=0;
    do{
        cout<<str[j]<<"\n";
         j++;
    }while(j<len);


    getch();
    return 0;
}

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

Output

 

Program 8

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

int main()
{
    string str[20];
    int len;
    cout<<"Enter array length\n";
    cin>>len;

    cout<<"\nEnter students name\n";
    int i=0;
    do{
         cout<<"Enter the string"<<" str=["<<i<<"]: ";
        cin>>str[i];
         i++;
    }while(i<len);

    cout<<"\nEntered students names are:\n";
    int j=0;
    do{
            cout<<"Your entered string is:"<<" str=["<<j<<"]: ";
        cout<<str[j]<<"\n";
         j++;
    }while(j<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

Data type in C++ language

Variable in C++ language

Operator in C++ language

Program to read and print string of one dim array in Java
Read and print string of one dim array in C
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

How to find reverse number using method in Java

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

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

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago