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 ofC++ 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 languageusing 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
#include
using namespace std;
int main()
{
char str[20];
cout<<"Enter your name\n";
cin>>str;
cout<<"\nyou entered:\n"<
When the above code is executed, it produces the following result
Enter your name:
Kamalakumar
You entered :
Kamalakumar
Program 2
#include
#include
using namespace std;
int main()
{
char str[20];
cout<<"Enter your name\n";
cin.get(str,20);
cout<<"\nCheck your name:\n"<
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
#include
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>str[i];
}
cout<<"\nEntered students names are:\n";
for(int i=0; i
When the above code is executed, it produces the following result
Output
Program 4
#include
#include
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>str[i];
}//Enter the students name using for loop
cout<<"\nEntered students names are:\n";
for(int i=0; i
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
#include
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>str[i];
i++;
}
cout<<"\nEntered students names are:\n";
int j=0;
while(j
When the above code is executed, it produces the following result
Output
Program 6
#include
#include
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>str[i];
i++;
}//Enter the student name using while loop
cout<<"\nEntered students names are:\n";
int j=0;
while(j
When the above code is executed, it produces the following result
Output
Read and print strings In an array using do-while loop
#include
#include
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
When the above code is executed, it produces the following result
Output
Program 8
#include
#include
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=["<>str[i];
i++;
}while(i
When the above code is executed, it produces the following result