Cities Database C# Sharp Exercise

Exercise

Cities Database

Objetive

Create a database to store information about cities.

In a first approach, we will store only the name of each city and the number of inhabitants, and allocate space for up to 500 cities.

The menu should include the following options:
1 .- Add a new city (at the end of the existing data)
2 .- View all cities (name and inhabitants)
3 .- Modify a record (rename and / or change number of inhabitants)
4 .- Insert a new record (in a specified position, moving the following ones to the right)
5 .- Delete a record (moving the following ones to the left so that no empty spaces are left)
6 .- Search in the records (display the ones which contain a certain text in their name, whether in upper or lower case, using partial search)
7 .- Correct the capitalization of the names (turn into uppercase the first letter and the ones after a space, and make the rest lowercase).
0 .- Exit

Example Code

using System;
public class exercise86
{

    struct city
    {
        public string name;
        public uint inhabitants;
    }

    public static void Main()
    {
        int maxCities= 500;
        city[] cities = new city[maxCities];
        int amount = 0;
        int currentCityNumber;        
        string option;                
        string textToSearch;
        bool found;
        string textToModify;
        bool finished = false;        

        do
        {
            Console.WriteLine();
            Console.WriteLine("Cities database");
            Console.WriteLine();
            Console.WriteLine("1.- Add a new city");
            Console.WriteLine("2.- View all cities");
            Console.WriteLine("3.- Modify a record");
            Console.WriteLine("4.- Insert a new record");
            Console.WriteLine("5.- Delete a record");
            Console.WriteLine("6.- Search in the records");
            Console.WriteLine("7.- Correct the capitalization of the names");
            Console.WriteLine("0.- Exit");
            Console.WriteLine();
            Console.Write("Choose an option: ");
            option = Console.ReadLine();

            switch (option)
            {
                case "0":
                    finished = true;
                    break;

                case "1":
                    if (amount > maxCities- 1)
                        Console.WriteLine("the database is full");
                    else
                    {
                        Console.WriteLine("Entering data for city number {0}", amount + 1);
                        Console.Write("Enter the city name: ");
                        cities[amount].name = Console.ReadLine();
                        Console.Write("Enter the inhabitants numbers: ");
                        cities[amount].inhabitants = Convert.ToUInt32(Console.ReadLine());
                        Console.WriteLine("The data was entered correctly");
                        amount++;
                    }
                    break;

                case "2":
                    for (int i = 0; i < amount; i++)
                    {
                        Console.WriteLine("{0}: {1}, {2} inhabitants",
                        i + 1, cities[i].name, cities[i].inhabitants);
                    }
                    Console.WriteLine();
                    break;

                case "3":
                    Console.Write("Enter the city number: ");
                    currentCityNumber = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter a new data for a city number: {0}", currentCityNumber);
                    Console.Write("City name (was {0}; hit ENTER to leave as is): ", cities[currentCityNumber - 1].name);
                    textToModify = Console.ReadLine();
                    if (textToModify != "")
                        cities[currentCityNumber - 1].name = textToModify;
                        Console.Write("Inhabitants (was {0}; hit ENTER to leave as is): ",
                        cities[currentCityNumber - 1].inhabitants);
                        textToModify = Console.ReadLine();
                        if (textToModify != "")
                            cities[currentCityNumber - 1].inhabitants = Convert.ToUInt32(textToModify);
                            Console.WriteLine();
                            break;
                case "4":
                    if (amount > maxCities- 1)
                        Console.WriteLine("The database is full");
                    else
                    {
                        Console.Write("Enter the number of the city to modify: ");
                        currentCityNumber = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Insert a new data at {0} position: ",currentCityNumber);
                        amount++;
                        for (int i = (int)amount; i > currentCityNumber - 1; i--)
                        {
                            cities[i] = cities[i - 1];
                        }
                        Console.Write("City name: ");
                        cities[currentCityNumber - 1].name = Console.ReadLine();
                        Console.Write("Inhabitants: ");
                        cities[currentCityNumber - 1].inhabitants = Convert.ToUInt32(Console.ReadLine());
                    }
                    break;

                case "5":
                    Console.Write("Enter the city number for delete: ");
                    currentCityNumber = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Deleting the number {0}", currentCityNumber);
                    for (int i = currentCityNumber - 1; i < amount; i++)
                    {
                        cities[i] = cities[i + 1];
                    }
                    amount--;
                    break;

                case "6":
                    Console.Write("Enter the text to search: ");
                    textToSearch = Console.ReadLine();
                    found = false;
                    for (int i = 0; i < amount; i++)
                    {
                        if (cities[i].name.ToUpper().IndexOf(textToSearch.ToUpper()) >= 0)
                        {
                            Console.WriteLine("{0} found in {1}",
                            textToSearch, cities[i].name);
                            found = true;
                        }
                    }
                    if (!found)
                        Console.WriteLine("Not found.");
                        break;

                case "7":
                    for(int i = 0;i < amount;i++)  
                    {
                        string lowerCaseName = cities[i].name.ToLower();
                        string correctedName = lowerCaseName.Substring(0, 1).ToUpper()
                        + lowerCaseName.Substring(1);
                        for (int j = 1; j < correctedName.Length - 2; j++)
                        {
                            if (correctedName[j] == ' ')
                            correctedName = correctedName.Substring(0, j) + " " +
                            correctedName.Substring(j + 1, 1).ToUpper() +
                            correctedName.Substring(j + 2);
                        }
                        cities[i].name = correctedName;
                    }
                    break;

                default:
                    Console.WriteLine("Wrong option ");
                    break;

            }

        } 
        while (!finished);
    }
}

More C# Sharp Exercises of Arrays, Structures And Strings

Reverse Array
Create a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
Search In Array
Create a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he...
Array Of Even Numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
Array Of Positive And Negative Numbers
Create a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
Many Numbers And Sum
Create a program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must d...
Two Dimensional Array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
Statistics V2
reate a statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been entere...
Struct
Create a "struct" to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
Array Of Struct
Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first...
Array Of Struct And Menu
Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for one point - Display al...
Books Database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
Centered Triangle
Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
Banner
Create a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
Triangle Right Side
Create a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
Strings Manipulation
Create a C# program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Disp...
Nested Structs
Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and ...
Sort Data
Create a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
Two Dimensional Array As Buffer For Screen
Create a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays th...
Two Dimensional Array 2: Circunference On Screen
Create a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it ...
Computer Programs
Create a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category ...
Exercise Tasks
Create a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month an...
Household Accounts
Create a program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), shou...