Programming Examples
Cpp program to input 5 cities, display the name and length of city with maximum length using array of strings
Write a program to input 5 cities, display the name and length of city with maximum length using array of strings
Solution
#include <iostream>
#include <string>
using namespace std;
int main()
{
string city[10],str=" "; // array of strings
char ch;
int size, max = 0;
cout << "Enter the cities" << endl;
for (int a = 0; a < 5; a++)
{
getline(cin, city[a]);
}
for (int a = 0; a < 5; a++)
{
size =city[a].length();
if(max<=size)
{
max=size;
str=city[a];
}
}
cout<<"The largest string is : "<<str<<" with length "<<max;
return 0;
}
Output
Enter the cities
kanpur
ahmedabad
new delhi
agra
banglore
The largest string is : new delhi with length 9