Question

This is for open data structures C++ 1. Read the entire input one line at a...

This is for open data structures C++ 1. Read the entire input one line at a time. Then output all lines sorted by length, with the shortest lines first. In the case where two lines have the same length, resolve their order using the usual “sorted order.” Duplicate lines should be printed only once.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>
#include <string>
#include <cstring>
#include<stdlib.h>
#define MAX 100
using namespace std;

// Function to read the each line from a file and stores it in string array line
int readFile(string line[])
{
// To refer to current index position
int index = 0;
// ifstream object declared
ifstream rFile;

// Opens the file for reading
rFile.open("stringData.txt");

// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<"\n Error: Unable to open the file! ";
exit(0);
}// End of if condition

// Loops till end of file
while(!rFile.eof())
// Reads each line of data from file and stores it at current index position
// increase the index by one
getline(rFile, line[index++]);
// Close the file
rFile.close();

// Returns number of years
return index;
}// End of function

// Function to display each line
void displayLine(string line[], int len)
{
// Loops till length of the array
for(int x = 0; x < len; x++)
// Displays each line of data with line number
cout<<"\n Line "<<(x + 1)<<": "<<line[x];
}// End of function

// Function to sort each line of data based on the each line length
void sortLine(string line[], int len)
{
// Loops till number of lines
for(int r = 0; r < len; r++)
{
// Loops till number of lines minus outer loop value
for(int c = 0; c < len - r - 1; c++)
{
// Checks if current line length is greater than the next line length
if(line[c].length() > line[c + 1].length())
{
// Swapping process
string temp = line[c];
line[c].assign(line[c + 1]);
line[c + 1].assign(temp);
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// main function definition
int main()
{
// Declares a string array of size MAX
string line[MAX];
// To store number of lines
int len = 0;
// Calls the function to read file and returns number of lines
len = readFile(line);
cout<<"\n Before Sorting based on line length\n";
// Calls the function to display each line of data
displayLine(line, len);

// Calls the function to sort
sortLine(line, len);
cout<<"\n\n After Sorting based on line length\n";
// Calls the function to display each line of data
displayLine(line, len);
}// End of main function

Sample Output:

Before Sorting based on line length

Line 1: This is a demo.
Line 2: Read the file contents.
Line 3: Count length of each line.
Line 4: Display each line.
Line 5: Sort each line based on the length.
Line 6: Display the biggest line.

After Sorting based on line length

Line 1: This is a demo.
Line 2: Display each line.
Line 3: Read the file contents.
Line 4: Display the biggest line.
Line 5: Count length of each line.
Line 6: Sort each line based on the length.

stringData.txt file contents

This is a demo.
Read the file contents.
Count length of each line.
Display each line.
Sort each line based on the length.
Display the biggest line.

Add a comment
Know the answer?
Add Answer to:
This is for open data structures C++ 1. Read the entire input one line at a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • C programming. please include comments In this lab, you will learn to read data from and...

    C programming. please include comments In this lab, you will learn to read data from and write data to a file - another use for pointers. SAMPLE PROGRAM There are two sample programs this week - WriteFile.c and ReadFile.c. The first, WriteFile.c, allows the user to enter customer names (first, middle, and last), and writes each name to an output text file, customerNames.txt. formatted as: firstName middlelnitial lastName" After you've run it once, what happens to the names you added...

  • In this stage you should read all of the data into internal structures suitable for use in the later stages and create...

    In this stage you should read all of the data into internal structures suitable for use in the later stages and create an output representation that provides an overview of the data that was read. Photo 1&2 - Question Photo 3 - Input data Photo 4 - Introduction please write a code that will execute such function & generate a required output (as stated on the question). Input data is not required to write a code but if you need...

  • Read an arbitrary number of positive integer values from stdin. The values are separated by one...

    Read an arbitrary number of positive integer values from stdin. The values are separated by one or more spaces or newlines (only). The input is guaranteed to be well-formed. On standard output, render a simple graph representation of the input values, in order, using hash # characters as shown in the examples below. The number of hashes printed should be equal to the input value. Your program should output exactly one line per input value. The value zero should generate...

  • Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values...

    Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values from stdin, separated by one or more spaces or newlines, until reaching EOF 2. The input is guaranteed to be well-formed. 3. The input contains no more than 80 values. 4. on standard output, render a simple vertical column graph representation of the input values, in order left to right, using hash'#' characters as shown in the examples below. The number of hashes printed...

  • write the bash script Write a script compress_large_files.sh. This script accepts one or more command line...

    write the bash script Write a script compress_large_files.sh. This script accepts one or more command line arguments. The first argument has to be an integer; let’s call it size. If this is the only command line argument, compress_large_files.sh inspects all files in the current working directory and compresses every file of size at least size. If there is more than one command line argument, all arguments except the first one must be valid directories. In this case, compress_large_files.sh inspects the...

  • In C, Write a program that can accept an arbitrary number of command line arguments, e.g....

    In C, Write a program that can accept an arbitrary number of command line arguments, e.g. program must do the following: program needs to split each of the arguments into two halves based on the length of a string. Specifically, the first half will be [0, n/2) and the second half will be [n/2] where n is the length of the string and n > 1. For instances, hello is split into two halves: he and llo. Similarly, world! is...

  • The program will need to accept two input arguments: the path of the input file and...

    The program will need to accept two input arguments: the path of the input file and the path of the output file. See etc/cpp/example.ifstream.cpp for the basis of how to do this. Once the input and output file paths have been received, the program will need to open the input and output files, read and process each input file line and create a corresponding output file line, close the input and output files, and then create and output some summary...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT