Reputation: 458
Background:
This questions comes from the Daily Coding problem #29.
Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.
Attempted Solution:
#include <iostream>
#include <string>
#include <vector>
std::vector<char> run_length(std::string str)
{
std::vector<char> result;
if (str.empty() || str.size() == 1)
{
const char *ch = str.c_str();
result.push_back(*ch);
return result;
}
int count = 1;
for (int i = 1; i < str.size(); ++i)
{
if (str[i] == str[i - 1])
count++;
else
{
if (count > 1)
{
char ch = count;
result.push_back(ch);
}
result.push_back(str[i - 1]);
count = 1;
}
}
if (count > 1)
{
char ch = count;
result.push_back(ch);
}
result.push_back(str[str.size() - 1]);
return result;
}
int main()
{
std::string str = "AAAABBBCCAA";
auto result = run_length(str);
for (auto it : result)
std::cout << it << " ";
std::cin.get();
}
Expected Output:
4A3B2C1D2A
Actual Output:
A B C A
Question:
Why do I get these strange characters for the actual output? I believe the logic of my approach should work to solve the problem but yet I get these characters that I have not seen before. Any suggestions are greatly appreciated.
Upvotes: 0
Views: 341
Reputation: 458
With some slight modification to the container and using std::to_string() I was able to get it to run perfectly.
// Daily coding problem #29
// Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as
// a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
// Implement run - length encoding and decoding.You can assume the string to be encoded have no digits and consists solely of alphabetic
// characters.You can assume the string to be decoded is valid.
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> run_length(std::string str)
{
std::vector<std::string> result;
if (str.empty() || str.size() == 1)
{
result.push_back(str);
return result;
}
int count = 1;
for (int i = 1; i < str.size(); ++i)
{
if (str[i] == str[i - 1])
count++;
else
{
if (count > 1)
{
result.push_back(std::to_string(count));
}
result.push_back((std::string(1, str[i - 1])));
count = 1;
}
}
if (count > 1)
{
result.push_back(std::to_string(count));
}
result.push_back(std::string(1, str[str.size() - 1]));
return result;
}
int main()
{
std::string str = "AAAAAAAAAABBBCCAA";
auto result = run_length(str);
for (auto it : result)
std::cout << it << " ";
std::cin.get();
}
Upvotes: 0
Reputation: 206567
The line
char ch = count;
is not correct.
If count
is 4, then ch
is initialized to a character that is encoded by the integer value 4. You need to get the character that represents the digit. You need '4'
. You can use the following to get the digit from count
.
char ch = '0' + count;
However, if count
is greater than 9, that won't work. You'll have to come up with a different strategy if you expect count
to be greater than 9.
Upvotes: 4