C with structures and string In this program you are going t
C++ with structures and string
In this program you are going to simulates the DNS by using a structure called MyHost. The information in MyHost structure includes the host name and its IP address.
To create the DNS database, you need to read the host names and the corresponding IP addresses from a file. The file has the following format:
Number_Hosts_in_Database
Host_Name IP_Address
Host_Name IP_Address .. .. ..
And so on
You can use the provided DSN.txt file to create a dynamic array of MyHost structure and store the information about all given hosts.
When the program runs, it should prompt the user to enter a host name and query the database for the corresponding IP address. If the host exists, display the host name and its IP address on the screen. Otherwise, inform the user that the host name does not exist in the database. The user should be able to search as many hosts as he/she would like ( your program should not quit after the first host!).
DSN.txt file contains:
www.hellotamu.com 111.115.14.27
 www.hellocosc36.com 168.143.199.41
 www.aggies2.com 242.244.94.55
 www.goaggies.com 28.30.120.51
 www.whitetamu.edu 210.21.205.183
 www.maroontamu.com 42.186.174.12
 www.cosc1436.com 66.44.172.169
 www.hellowhite.com 178.27.160.159
 www.hellomaroon.com 242.176.138.168
 www.gotamu.org 20.208.30.151
 www.codetamu.org 245.112.154.68
 www.mytamu.edu 252.218.111.238
 www.theutuni.com 187.214.135.204
 www.seemaroon.com 81.185.145.61
 www.byered.com 72.169.132.211
Solution
import java.io.*;
 import java.util.*;
//Class Dns lookup which will read the input from txt file and stores in a map.When a lookup neeeds it retrieves the data from the Map
 public class DnsLookup {
   
    //Driver method main
    public static void main(String[] args) throws IOException{
        FileInputStream fis = null;
       
         BufferedReader input = null;
                 
          //Provide your File input path where the input file is present
         fis = new FileInputStream(\"C:\\\\Users\\\\ksumanth\\\\Documents\\\\input.txt\");
         input = new BufferedReader(new InputStreamReader(fis));
       
         String line = null;
         int lineNo = 1;
         //Map to store the Url and the IP Address Mapping
         Map<String,String> map = new HashMap<String,String>();
         String lookup[] = new String[2];
         int i = 0;
       
         //loop through all the lines in the input.txt and stores key value pairs in a Map
         while(i <= 14){
             lookup = input.readLine().split(\" \");
             map.put(lookup[0], lookup[1]);
             i++;
         }
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
         int flag = 1;
       
       
         while(flag > 0){
           
         System.out.println(\"Please Enter the Host Name :-    \");
         String url = br.readLine().trim();
       
         //Whether the enetered String contains in the map or not it will check.
         if(map.containsKey(url)){
            String ipAddress = map.get(url);
            System.out.println(\"The IP address for the above Url is :-    \"+ipAddress+\"\ \");
         }else{
            System.out.println(\"The Url was not present in the database\ \");
         }
         //It will asks whether we need an another lookup of the url
         System.out.println(\"Do you want the IP address of any other url?\ Enter y or n:- \ \");
         String task = br.readLine().trim();
       
         //Enter y to Enter the Url again for lookup. If noy entered \'y\' terminate.
         if(task.charAt(0) != \'y\'){
            flag = -1;
         }
       
      
         }
       
}
}
//Class MyHost like Struct in C++
 class MyHost{
    String hostName;
    String ipAddress;
 }
Output:-
Please Enter the Host Name :-  
 www.hellocosc36.com
 The IP address for the above Url is :-    168.143.199.41
Do you want the IP address of any other url?
 Enter y or n:-
y
 Please Enter the Host Name :-  
 www.maroontamu.com
 The IP address for the above Url is :-    42.186.174.12
Do you want the IP address of any other url?
 Enter y or n:-
y
 Please Enter the Host Name :-  
 www.gotamu.org
 The IP address for the above Url is :-    20.208.30.151
Do you want the IP address of any other url?
 Enter y or n:-
y
 Please Enter the Host Name :-  
 www.aggies2.com
 The IP address for the above Url is :-    242.244.94.55
Do you want the IP address of any other url?
 Enter y or n:-
y
 Please Enter the Host Name :-  
 www.gotamu.org         20.208.30.151
 The Url was not present in the database
Do you want the IP address of any other url?
 Enter y or n:-
n



