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

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz