Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A top-level domain (TLD) name is the last part of an Internet domain name like .

ID: 3667568 • Letter: A

Question

A top-level domain (TLD) name is the last part of an Internet domain name like .com in example.com. A core generic top-level domain (core gTLD) is a TLD that is either .com, .net, .org, or .info. A second-level domain is a single name that precedes a TLD as in apple in apple.com

The following program uses a loop to repeatedly prompt for a domain name, and indicates whether that domain name consists of a second-level domain followed by a core gTLD. An example of a valid domain name for this program is apple.com. An invalid domain name for this program is support.apple.com because the name contains two periods. The program ends when the user presses just the Enter key in response to a prompt.

Run the program and enter domain names to validate. Note that even valid input is flagged as invalid.

Change the program to validate a domain name. A valid domain name for this program has a second-level domain followed by a core gTLD. Run the program again.

#include <iostream>
#include <string>
using namespace std;

int main() {
   string inputName = "";
   string searchName = "";
   string coreGtld1 = ".com";
   string coreGtld2 = ".net";
   string coreGtld3 = ".org";
   string coreGtld4 = ".info";
   string theTld     = "";
   bool isCoreGtld   = false;
   // FIXME: Add variable periodCounter to count periods in a domain name
   int periodPosition = 0; // Position of the period in the domain name

   int j = 0;

   cout << endl << "Enter the next domain name (<Enter> to exit): " << endl;
   cin >> inputName;

   while (inputName.length() > 0) {
      searchName = inputName;
      for (j = 0; j < inputName.length(); ++j) {
         searchName.at(j) = tolower(inputName.at(j));
      }
      isCoreGtld = false;

      // FIXME: Write a for loop using variable i to store in periodCounter the
      //        number of periods in searchName. Store the position of the period in
      //        PeriodPosition. If searchName has exactly one period and searchName's
      //        first character is not a period, determine whether searchName has a
      //        valid core gTLD by extracting the domain name into variable coreGtld
      //        and comparing the name with valid core gTLDs.

      // Extract the top-level domain name starting at the period's position.
      // Ex: If searchName = "example.com", the next statement extracts ".com"
      theTld = searchName.substr(periodPosition);

      if (theTld == coreGtld1) {
         isCoreGtld = true;
      }
      else if (theTld == coreGtld2) {
         isCoreGtld = true;
      }
      else if (theTld == coreGtld3) {
         isCoreGtld = true;
      }
      else if (theTld == coreGtld4) {
         isCoreGtld = true;
      }
      else {
         isCoreGtld = false;
      }
     
      cout << """ << inputName << "" ";
      if (isCoreGtld) {
         cout << "is a second-level domain followed by a core gTLD of "" <<
                 theTld << ""." << endl;
      }
      else {
         cout << "is not a second-level domain followed by a core gTLD." << endl;
      }

      cout << endl << "Enter the next domain name (or <Enter> to exit): " << endl;
      inputName = "";
      cin >> inputName;
   }

   return 0;
}

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;

int main() {
   string inputName = "";
   string searchName = "";
   string coreGtld1 = ".com";
   string coreGtld2 = ".net";
   string coreGtld3 = ".org";
   string coreGtld4 = ".info";
   string theTld     = "";
   bool isCoreGtld   = false;
   // FIXME: Add variable periodCounter to count periods in a domain name
   int periodPosition = 0; // Position of the period in the domain name

   int j = 0;

   cout << endl << "Enter the next domain name (<Enter> to exit): " << endl;
   cin >> inputName;

   int countDot; // count number of '.'
   while (inputName.length() > 0) {
      searchName = inputName;
      countDot = 0; //initializing at start
      periodPosition = -1;
      for (j = 0; j < inputName.length(); ++j) {
         searchName.at(j) = tolower(inputName.at(j));
         if(searchName.at(j)=='.'){
           periodPosition = j;
           countDot++;
       }
      }
      isCoreGtld = false;

      // FIXME: Write a for loop using variable i to store in periodCounter the
      //        number of periods in searchName. Store the position of the period in
      //        PeriodPosition. If searchName has exactly one period and searchName's
      //        first character is not a period, determine whether searchName has a
      //        valid core gTLD by extracting the domain name into variable coreGtld
      //        and comparing the name with valid core gTLDs.

      // Extract the top-level domain name starting at the period's position.
      // Ex: If searchName = "example.com", the next statement extracts ".com"
      theTld = searchName.substr(periodPosition);
      cout<<"theTls value: "<<theTld<<endl;
      cout<<"position value: "<<periodPosition<<endl;
      cout<<"countDot value: "<<countDot<<endl;


      if (theTld == coreGtld1) {
         isCoreGtld = true;
      }
      else if (theTld == coreGtld2) {
         isCoreGtld = true;
      }
      else if (theTld == coreGtld3) {
         isCoreGtld = true;
      }
      else if (theTld == coreGtld4) {
         isCoreGtld = true;
      }
      else {
         isCoreGtld = false;
      }
   
      cout << """ << inputName << "" ";
      if (isCoreGtld &&(countDot == 1)) {
         cout << "is a second-level domain followed by a core gTLD of "" <<
                 theTld << ""." << endl;
      }
      else {
         cout << "is not a second-level domain followed by a core gTLD." << endl;
      }

      cout << endl << "Enter the next domain name (or <Enter> to exit): " << endl;
      inputName = "";
      cin >> inputName;
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote