Reputation: 462
The question is: The character L repeats 20 times in a text file i.e. somewhere in the file we have LLLLLLLLLLLLLLLLLLLL. It takes 20 bytes to store this ‘run’ of L. However, if we write 20L in the file it takes much less. But 20 is not a character. It is a number and we don’t want to write numbers in a text file. There is another way out. Let us use the capital letters to represent the runs i.e. if L occurs once we write AL, if twice, we write BL and so on. So we write TL for 20 occurrences of L. This method can code only upto 26 occurrences. If a character occurs more then we can write one more code for it. Thus, in the coded file, for saving space, a string of DfFAB-ZsAsD AA stands for ffffAAAAAA—sssssssssssssssssssssssssss A. Write a program that reads from a text file and compresses it using this method.
My attempt:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter input(max. 99 characters): ");
char szInput[100];
char chInput;
int iii = 0;
do
{
chInput = getchar();
szInput[iii] = chInput;
iii++;
} while (chInput != '\n');
szInput[iii--] = '\0';
char *szOutput = malloc(2 * (iii + 1) * sizeof(char));
iii = 0;
int jjj = 0;
while (szInput[iii] != '\0')
{
int nCount = 1;
while (szInput[iii + nCount] == szInput[iii] && nCount < 26)
{
nCount++;
}
szOutput[jjj] = nCount + 64;
szOutput[++jjj] = szInput[iii];
iii += nCount;
jjj++;
}
szOutput[jjj] = '\0';
printf("%s", szOutput);
return 0;
}
When I give an input "eee" or "eeeee", the output is CeA and EeA respectively. It prints an extra A in the end. I can't find the error in my code.
Upvotes: 0
Views: 68
Reputation: 4366
Your problem is this :
szInput[iii--] = '\0';
This is not overwriting the \n
You should write:
szInput[--iii] = '\0';
Upvotes: 3