Я пытаюсь написать программу кодирования длин серий в C.
Для ввода 'ABBCD' я ожидаю следующий результат: 'A1B2C1D1'
Я сдаю два- строка массива char для строки функции, которая кодирует символы:
for(i; i <= curline; i++) //hand over line for line
{
encoded->lines[i] = malloc(255);
encoded->lines[i] = rle_encode(read->lines[i]); //read->lines contains the characters of each line
printf("%s", encoded->lines[i]); // print out the result returned by the function rle_encode
}
Я проверил это и знаю, что это будет работать.
Теперь это моя функция. Rle_encode:
char *rle_encode(char *line){
char *encode = malloc(sizeof(2 * strlen(line) + 1));
char prev = line[0]; //here I want to save the previous character
int i = 0;
int z = 1;
do{
i++;
if(prev == line[i]) // if character n equals n-1 (previous)
{
z++; // increase counter varaible z
}else
{
strcat( encode, line[i] ); //the content of line[i] will be append to the array encode
strcat( encode, z ); //also the counter variable will be appended
}
prev = line[i];
}while(line[i] != '\n'); //in the end of each line a '\n' appears, if line[i] is '\n' it should stop the function
return encode;}
Что не так в функции rle_encode
?