[edit]
It seems that
korno917 has entitlement issues on a public forum, so good riddance.
His homework was to take a string of alphabetic letters and do some run-length encoding. For example: “aaabcc” → “3ab2c”.
Here’s a recursive version using ONLY STRINGS, LOL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <sstream>
#include <string>
std::string compress( const std::string& s )
{
if (s.empty()) return "";
auto n = s.find_first_not_of( s[0] );
if (n == s.npos) n = s.size();
if (n > 1) return std::to_string( n ) + s[0] + compress( s.substr( n ) );
else return s[0] + compress( s.substr( n ) );
}
int main()
{
std::string data;
while (std::cin >> data)
{
std::cout << "The compressed data is: " << compress( data ) << "\n";
}
}
|
Turn
that in and see if your prof fails you for academic dishonesty.
My original response follows.
[/edit]
You have named your data string “runLength”. It is not the run length. It is the data. JSYK.
If you are using
cin >> data
then you are automatically breaking on spaces! So all you need is an outer loop to print your message.
1 2 3 4
|
while (cin >> data) // as long as there is space-separated input...
{
std::cout << "The compressed data is: " << compress( data ) << "\n";
}
|
BTW, no need for vectors. A string is nicer to use than a vector<char>.
I do not understand why you need a separate string with only “aea” in it. This does not represent the data in any way. Have I misunderstood a need?
BTW, it is
not cheating to ask for help.
It is cheating to have someone do your work for you.
Hope this helps.