Question

In: Computer Science

Objectives: Practice using files and strings. More practice using loops with files and strings Background Assume...

Objectives:

  • Practice using files and strings.
  • More practice using loops with files and strings

Background

Assume you're working on a contract where the company is building a e-mailing list. Your task is to write a C++ program that reads through an e-mail stored in the current working directory, called mail.dat, and outputs every email address found as individual lines to a file called email_list.dat. For this assignment, if a whitespace delimited string of characters has an embedded commercial at sign (@) inside it (that is, anywhere in the string), we shall consider it an e-mail address. (Yes, this is somewhat simplistic, but this is more about practice even though this allows an email to have an @ sign at the beginning/end of an address which would not be allowed in the "real world".) However, you will need to trim any trailing commas. Thus the string "[email protected]," must appear in the output file as "[email protected]" with the trailing comma removed. Only commas at the end of a string are considered trailing; do not remove non-trailing commas. Do not worry about any other punctuation characters; the only editing your program must do is to remove trailing commas.

Solutions

Expert Solution

C++ Program :

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

int main()
{
    string str;
    int n;

    ifstream in("mail.dat");   // opens input file
    ofstream out("email_list.dat"); // writes to output file

    while(in>>str)    // read the strings in file until end of file
    {
        n = str.length();  // length of word or string
        for(int i=0; i<n; i++)
        {
            if(str[i] == '@') // search for @ in a string in the input file
            {
                if(str[n-1] == ',') // if  a trailing comma is found then this will ommit it
                {
                    n=n-1; // trim the string by 1 character
                }
                for(int j =0; j<n; j++)
                {
                    out<<str[j]; // print the whole string which is email-address to output file
                }
                out<<"\n"; // print a next line in output file
            }
        }
    }
    return 0;
}

Screenshots and Outputs :

Code :

Input file :

mail.dat

Output file :

email_list.dat


Related Solutions

In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
Language: C++ 3 Campus Travel Game This lab will practice using loops. We will construct a...
Language: C++ 3 Campus Travel Game This lab will practice using loops. We will construct a short computer game. Please look at the Test Cases for exact wording. For your game, the player’s goal is to reach campus exactly. The player starts 14 miles away and has up to 4 turns to reach campus. At each turn the play can ride either use a Bus, a Subway, or a Jetpack: Riding a Bus moves the player forward 2 miles each...
context: 1A: Using the alligator clip wires, attach the coil with more loops to the galvanometer....
context: 1A: Using the alligator clip wires, attach the coil with more loops to the galvanometer. Look carefully at the direction that the wires are turned. The idea here is that you will be moving the pole of a magnet closer to the coil— increasing the magnetic field strength in the vicinity of the coil, which is one way to increase magnetic flux. Thinking about the orientation of your loops of wire, and using the appropriate right-hand rule(s), decide which...
3. How many strings can be made using 9 or more letters of MISSISSIPPI?
3. How many strings can be made using 9 or more letters of MISSISSIPPI?
Objective Work with strings Work with files Work with formatted output This program will display a...
Objective Work with strings Work with files Work with formatted output This program will display a very basic handling of transactions for a checking account. The program will read in a data file with all the past transactions. Use FileUtil.selectOpenFile or input() to get a file name from the user. The format of the file will be very basic, just a date (as a string in the format of MM/DD), a single letter which indicates the type of transaction (‘B’...
Learning Objectives Learn more about the statistical analysis tools in SPSS. Practice manual calculations and confirm...
Learning Objectives Learn more about the statistical analysis tools in SPSS. Practice manual calculations and confirm with SPSS output. Walk through all steps of hypothesis testing for a related-samples t-Test. About Your Data A statistics student was late on completing the final statistics project. While racking her brain for ideas, she watched her cat eating supper and noticed how much quicker the cat ate the new brand of cat food. Feeling desperate about the project, she decided to figure out...
Describe your practicum goals and objectives using the seven domains of practice Discuss a nursing theory...
Describe your practicum goals and objectives using the seven domains of practice Discuss a nursing theory that would be used to guide your practice.
Describe your practicum goals and objectives using the seven domains of practice Discuss a nursing theory...
Describe your practicum goals and objectives using the seven domains of practice Discuss a nursing theory that would be used to guide your practice.
Objectives: Practice using selection structures within a complete C++ program to solve a problem. Be able...
Objectives: Practice using selection structures within a complete C++ program to solve a problem. Be able to understand and use linear interpolation in a program. Modify/Extend an existing program. Problem Description: Linear Interpolation There are two credit levels for this assignment. Completing the “B” level correctly is worth 16/20 points, completing the “A” level is worth 20/20. You are encouraged to get the code for the “B” level working first and then complete the “A” level if you have time/interest....
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This assignment will give you experience on the use of the while loop and the for loop. You will use both selection (if) and repetition (while, for) in this assignment. Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT