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...
Using the alligator clip wires, attach the coil with more loops to the galvanometer. Look carefully...
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. Experiment 2: , but move the magnet much faster. What happens to your current (as measured by the meter)...
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
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?
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...
Goals Practice loops and conditional statements Description Create a game for elementary school students to practice...
Goals Practice loops and conditional statements Description Create a game for elementary school students to practice multiplication. The application should display a greeting followed by a set of mathematical questions.  In each set the player is asked to enter a number from 1 to 9 to test multiplication skills or to press ‘E’ to exit the game.  The application checks if the number entered is indeed between 1 and 9 and if not displays a message inviting the...
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’...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT