C# Program to Implement PhoneBook

This is a C# Program to implement phonebook.

Problem Description

This C# Program Implements PhoneBook.

Problem Solution

Reads a file with names and phone numbers and builds a phone book from it and Returns the phone number for a given name.

Program/Source Code

Here is source code to Implement PhoneBook.The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement PhoneBook
 */
using System;
using System.Collections;
using System.IO;
class PhoneBook
{
 
    static void Main(string[] arg)
    {
        Hashtable tab = new Hashtable();
        string fileName;
        if
        { 
            (arg.Length > 0) fileName = arg[0];
        } 
        else
        { 
            fileName = "phoneBook.txt";
        }
        StreamReader r = File.OpenText(fileName);
        string line = r.ReadLine();  
        while (line != null)
        {
            int pos = line.IndexOf('=');
            string name = line.Substring(0, pos).Trim();
            long phone = Convert.ToInt64(line.Substring(pos + 1));
            tab[name] = phone;
            line = r.ReadLine();
        }
        r.Close();
        for (; ; )
        { 
            Console.Write("Name : ");
            string name = Console.ReadLine().Trim();
            if (name == "") 
                break;
            object phone = tab[name];
            if (phone == null)
                Console.WriteLine("-- Not Found in Phone Book");
            else
                Console.WriteLine(phone);
        }
    }
}
Program Explanation

In C# Program, we are reading a file with names and phone numbers and build a phone book from it and return the phone number for a given name. If else condition statement is used to assign the value of ‘arg[0]’ variable to the file name.

advertisement
advertisement

Otherwise, if the condition is false then execute the else statement. Assign the phonebook text file to the file name variable. While loop is used to check that line variable value is not equal to null. If the condition is true then execute the iteration of the loop. In substring() function assign the name, from the file present in read mode.

Using ToInt64() function convert the text in number format to the phone variable. Then close the file. If else condition statement, is used to display the name and phone number from the phone book.

Runtime Test Cases
 
Name : Ram
9999945670
Name : Raj
-- Not Found in Phone Book

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.