Please help me with two issues I have on a program.

...
Last edited on
[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.
Last edited on
(1)
That's fine. Use vectors.

Create two of them: one for the compressed data, one for the letters. Every time you add a letter to the compressed data, also add it to the other. Every time you add a number, don't.

To add to the vectors, instead of using cout, just push_back().

1
2
3
4
5
6
7
8
9
10
11
std::vector <char> compressed_data;
std::vector <char> letters;

for (...)
{
  ...
  compressed_data.push_back( number_of_times );
  compressed_data.push_back( letter );
  letters.push_back( letter );
  ...
}


(2)
Correct.
(1)
Ah, just remember the last, and count how many duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// pseudocode

while (characters left)
{
  int count = 1;
  char last = current character;
  current++;
  while (current character == last)
  {
    count++;
    current++;
  }

  count → string.
  add each character of count string to compressed_data vector
  add last to compressed_data vector and letters vector
}

Is this not more-or-less what you had?

(2)
It works just fine. I gave you a working example — that assumed strings. Here is one assuming vectors:

1
2
3
4
5
6
7
8
9
while (cin >> data)
{
  vector<char> compressed_data, letters;
  compress( data, compressed_data, letters );
  cout << "The compressed data is: ";
  for (auto c : compressed_data)
    cout << c;
  cout << "\n";
}

This assumes a function that looks like:

1
2
3
4
void compress( string data, vector<char>& compressed_data, vector<char>& letters )
{
  ...
}
You could use a nested loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	string runLength;

	vector<int> lengths;
	vector<char> chars;

	cout << "Enter the data to be compressed: ";
	cin >> runLength;

	//Do Whatever You Need For WhiteSpace


	//"i" is to parse the string, "t" is keeping track of loop count
	for (int i = 0, t = 0; i < runLength.size(); i++, t++)
	{
		int track = 0; //See how many letters to skip in string

		lengths.push_back(1); //Every letter at least appears once
		chars.push_back(runLength[i]); //Add each letter

		for (int j = i + 1; j < runLength.size(); j++)
		{
			//BREAK if the next letter doesn't match
			if (runLength[i] != runLength[j]) 
				break;

			//If it does match, add to the length
			lengths[t]++;
			//And also keep track of how many times this happens
			track++;
		}
		//This is to skip letters we've already accounted for
		//If input is "aabb", you only want 1 loop for 'a' and 'b'
		i += track;
	}

	for (int i = 0; i < lengths.size(); i++)
		std::cout << lengths[i] << chars[i] << ' ';
}
Last edited on
Topic archived. No new replies allowed.