Question #325781

6.6.

Write a program using string functions that will accept the name of the country as input value and will display the corresponding capital. Here is the list of the countries and their

capitals.


COUNTRY CAPITAL

Canada Ottawa

United States Washington D.C.

U.S.S.R. Moscow

Italy Rome

Philippines Manila


Expert's answer

using System;

namespace ConsoleApp1
{
    class Program
    {
        public static string GetCapital(string country)
        {
            string[,] CountryCapital = new string[5, 2] {
                { "Canada", "Ottawa" },
                { "United States", "Washington D.C." },
                { "U.S.S.R.", "Moscow" },
                { "Italy", "Rome" },
                { "Philippines", "Manila" },
            };

            for (int i = 0; i < CountryCapital.GetLength(0); i++)
            {
                if (CountryCapital[i, 0].Equals(country, StringComparison.OrdinalIgnoreCase))
                    return CountryCapital[i, 1];
            }
            return "Unknown";
        }

        static void Main(string[] args)
        {
            string c1 = "Italy";
            Console.WriteLine("The capital of {0} is {1}.", c1, GetCapital(c1));

            string c2 = "Canada";
            Console.WriteLine("The capital of {0} is {1}.", c2, GetCapital(c2));

            string c3 = "France";
            Console.WriteLine("The capital of {0} is {1}.", c3, GetCapital(c3));

            string c4 = "Philippines";
            Console.WriteLine("The capital of {0} is {1}.", c4, GetCapital(c4));
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS