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
  • ***************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++)...

  • 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];...

  • 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...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • I have written this function in C++. A string of integers is passed in as dString...

    I have written this function in C++. A string of integers is passed in as dString and an integer is passed in as count. each dString integer is then indexed and passed into strTolnt integer array. each strTolnt integer is then passed through an equation and stored in temp array. I have printed each index in the array and the correct values are displayed, but when I print the entire array the value is displayed as hex. temp array is...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

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
ADVERTISEMENT