Skip to content

Instantly share code, notes, and snippets.

@jpmec
Last active December 21, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmec/6325804 to your computer and use it in GitHub Desktop.
Save jpmec/6325804 to your computer and use it in GitHub Desktop.
A simple run length encoding program.
#include <iostream>
using namespace std;
/// Simple run-length encoding program.
///
/// Example usage:
/// runlenc AAABBAAA
///
/// Result:
/// 3A2B3A
///
int main(int argc, char* argv[])
{
if (argc != 2)
{
cerr << "expected runlenc string" << endl;
return -1;
}
char* c_ptr = argv[1];
char c = *c_ptr;
char b = c;
size_t n = 0;
while (c)
{
if ((b != c) || (n == 9))
{
cout << n << b;
b = c;
n = 0;
}
else
{
++n;
++c_ptr;
c = *c_ptr;
}
}
cout << n << b;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment