Question

I need only one  C++ function . It's C++ don't write any other language.

Hello I need unzip function here is my zip function below. So I need the opposite function unzip.

Instructions:

The next tools you will build come in a pair, because one (zip) is a file compression tool, and

the other (unzip) is a file decompression tool.

The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when you encounter n characters of the same type in a row, the compression tool (zip) will turn that into the number n and a single instance of the character.

Thus, if we had a file with the following contents: 10a4b
the tool or unzip Requirements wcat is passed in a file and will output the file on the terminal (use cat to familiarize yourself with how thisfunction would turn it (logically) into:
aaaaaaaaaabbbb

So write the C++ unzip function that will turn 10a4b into aaaaaaaaaabbbb opposite of zip function. I'll make a txt file example like file.txt that will have 10a4b text once I will run your unzip function it will turn into aaaaaaaaaabbbb. I tested my zip function on terminal and it's working perfectly.

here is my zip function:

Full program with zip funcion with output

#include<iostream>
#include<string>
#include<fstream>

using namespace std;
string zip(const string & str)
{
   int i = str.size();
   string enc;

   for (int j = 0; j < i; ++j) {
       int count = 1;
       while (str[j] == str[j + 1]) {
           count++;
           j++;
       }
       enc += std::to_string(count);
       enc.push_back(str[j]);
   }
   return enc;
}
int main(int argc, char *argv[])
{
   ifstream in;
       // one argument
       if (argc<2) {
           cout << "Please enter command";
               return(-1);
       }
   // two arguments
   if (argc<3) {
       cout << "Please enter file name";
       return(-1);
   }
   in.open(argv[2]);
   if (in.bad()) {
       cout << "File not found";
       return(-1);
   }
   string line;
   // read each line
   while (getline(in, line))
   {
       // decode line
       string encode = zip(line);
       // print line
       cout << encode << endl;
   }
   return(0);
}

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

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string unzip(const string & str)
{
int i = str.size(),l=0;
string dec;
for (int j = 0; j < i; ++j) {
if(isdigit(str[j]))
l=l*10+(str[j]-'0');
else
{
for(int k=0;k<l;++k)
dec.push_back(str[j]);
l=0;
}
}
return dec;
}
int main(int argc, char *argv[])
{
ifstream in;
// one argument
if (argc<2) {
cout << "Please enter command";
return(-1);
}
// two arguments
if (argc<3) {
cout << "Please enter file name";
return(-1);
}
in.open(argv[2]);
if (in.bad()) {
cout << "File not found";
return(-1);
}
string line;
// read each line
while (getline(in, line))
{
// decode line
string decode = unzip(line);
// print line
cout << decode << endl;
}
return(0);
}

Add a comment
Know the answer?
Add Answer to:
I need only one  C++ function . It's C++ don't write any other language. Hello I need...
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
  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • I am getting a seg fault with my huffman tree. I'm not sure if it's my...

    I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public:     // constructor with left and right NULL nodes     Node(char charTemp, int c) {       ch = charTemp;       count = c;       left...

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