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

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2024 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