Thursday, February 6, 2020

A simple and stupid PhoneBook in C#

I just completed a HackerRank Challenge today. the topic is about to write a program for a phonebook which allow the user to input contact information and support searching contact by name.

initial i finished it only support number of input search.Finally i have to use the while to support unlimited number of search..


here is the snippet of code. i hope you will enjoy it

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution */
Dictionary<string, string> phoneBook=new Dictionary<string, string>();
int totalContact=Int32.Parse(Console.ReadLine());
for(var i=0; i<totalContact; i++){
string input=Console.ReadLine();
string[] strArray=input.Split(null);
if(strArray.Length>=2){
phoneBook.Add(strArray[0], strArray[1]);
}
}
//use the while loop to support unspecific number of search
while(true){
try{
string key=Console.ReadLine();
if (phoneBook.ContainsKey(key))
{
Console.WriteLine($"{key}={phoneBook[key]}");
}
else
{
Console.WriteLine("Not found");
}
}catch(System.Exception ex){
break;
}
}
}
}

cheer

No comments:

Post a Comment