I am trying to write a function compress() that takes in an input of a 2-dimensional array (5x10) of character
data, compresses each row of the array by replacing each run of character with a single character
and the number of times it occurs, and prints on each line the result of compression in the
function. For example, the row with data aabbcdddee may be compressed into a2b2c1d3e2.
Input (5x10 of characters):
aaacccdeee
sssseeeedd
aaaaaaaccc
eeedddasee
ddeeeeeggg
Output:
a3c3d1e3
s4e4d2
a7c3
e3d3a1s1e2
d2e5g3
I could achieve the compression part, but behind the compressed array, it printed out 5 extra lines of alien ASCI code, followed by printing out 5 times of everything .
Code:
void compress(char data[SIZE1][SIZE2])
{
int i,j, k,l ,count;
char data123;
printf("Enter your data (5x10 of characters): ");
for (i=0; i < SIZE1; i++) {
for (j=0; j < SIZE2; j++)
data[i][j] = getchar();
fflush(stdin);
}
printf("\n");
for (l=0;l<SIZE1;l++){
for(j = 0; j < SIZE2; j++)
{
count = 0;
printf("\n");
for(k = 0; k < SIZE2; k++)
{
count++;
if (k == SIZE2-1 || data[j][k] != data[j][k+1])
{
printf("%c %d ", data[j][k], count);
count = 0;
}
}
}
//printf("\n");
}
}