195k views
3 votes
Write a program whose input is an email address, and whose output is the username on one line and the domain on the second. Example: if the input is:

[email protected]

Then the output is

username: pooja

domain: piazza.com

The main program is written for you, and cannot be modified. Your job is to write the function parseEmailAddress defined in "util.cpp" The function is called by main() and passed an email address, and parses the email address to obtain the username and domain. These two values are returned via reference parameters. Hint: use the string functions .find() and .substr(),

main.cpp is a read only file

#include
#include

using namespace std;

// function declaration:
void parseEmailAddress(string email, string& username, string& domain);

int main()
{
string email, username, domain;

cout << "Please enter a valid email address> ";
cin >> email;
cout << endl;

parseEmailAddress(email, username, domain);

cout << "username: " << username << endl;
cout << "domain: " << domain << endl;

return 0;
}

/*util.cpp*/ is the TODO file

#include
#include

using namespace std;

//
// parseEmailAddress:
//
// parses email address into usernam and domain, which are
// returned via reference paramters.
//
void parseEmailAddress(string email, string& username, string& domain)
{
//
// TODO: use .find() and .substr()
//

username = "";
domain = "";

return;
}

User KernelM
by
3.5k points

1 Answer

7 votes

Answer:

1 void parseEmailAddress(string email, string& username, string& domain)

2 {

3 int found = email.find("@")

4 if (found > 0)

5 {

6 username = email.substr(0, found);

7 domain = email.substr(found+1, -1);

8 }

9 return;

10}

Explanation line by line:

  1. We define our function.
  2. We use an open curly bracket to tell the program that we are starting to write the function down.
  3. We apply the find method to the email variable that was passed by the main program. The find method tells us where is the "@" located within the email.
  4. We use an if statement to ensure that the value that we found is positive (The value is negative if an only if "@" is not in the email address).
  5. We use an open curly bracket to tell the program that we are starting to write inside the if statement.
  6. We apply the substr method to the email to take the username; it receives a start and an end value, this allows us to take from the beginning of the email (position 0) until the "@".
  7. We apply the substr method to the email to take the domain; it receives the position of the "@" character plus one to take the first letter after the "@" and a minus-one representing the last character on the email.
  8. We use a closing curly bracket to tell the program that the if statement has finished.
  9. We return nothing because we are using reference parameters, which means that the memory positions of username and domain are going to be filled by our parseEmailAddress function and the main function can access those values directly.
  10. We use a closing curly bracket to tell the program that the function has finished.
User Seyed Mohammad
by
3.7k points