How to write a C program to compress a String?

Today we are going to write a c program to compress a string and reduce the size of a string.

Let us look at the sample input and output.

Sample Input : aaccccabbbccaaaaa xxxxyyyxxxyyyyyyyyyya
Sample Output : a2c4ab3c2a5 x4y3x3y10a

First, we need to identify the pattern of the program.

  • Every character with repetitions replaces by the count of that character.
  • Suppose character occurs once we do not display the count as 1.We just output the number. It is also valid for space as well.
  • We get count by considering nearby characters, not as a whole string. If a character occurs again near in the string, then we count that together.

Study the below examples for further understanding.

aaa->a3
aaabb->a2b2
abbaaa->ab2a3
aaaba bbba->a3ba b3a

It is time for coding.😋

#include <stdio.h>
#include <string.h>
int main()
{
    char text[200];
    printf("enter input text :");
    scanf("%[^\n]s",text);
    char temp[strlen(text)+1];
    int counts[strlen(text)+1];
    int count=0,counter=0;
    for(int i=0;i<strlen(text);i++)
    {
        if(i==0)
        {
            temp[counter]=text[i];
            count++;
        }
        else if(text[i]!=text[i-1])
        {
            counts[counter]=count;
            count=0;
            count++;
            counter++;
            temp[counter]=text[i];;
        }
        else if(i==(strlen(text)-1))
        {
            if(text[i]!=text[i-1])
            {
                counts[counter]=count;
                count=0;
                count++;
                counter++;
                temp[counter]=text[i];;
                counts[counter]=count;
            }
            else
            {
                count++;
                counts[counter]=count;
            }
            counter++;
            temp[counter]='\0';



        }
        else
        {
            count++;
        }
    }

    for(int i=0;i<strlen(temp);i++)
    {
        if(counts[i]>1)
        {
            printf("%c%d",temp[i],counts[i]);
        }
        else
        {
            printf("%c",temp[i]);
        }

    }
    printf("\n");
}

The above code will do your work. Let us explain the program.

As usual, in the first and second lines, import required libraries.

In the fifth line, we define a char array named text to store user input. Be careful in this line. You need to declare an array that is enough to store user input; otherwise, you will get serval errors.

Then we get user input using the scanf function in line 7.Be careful to use “%[^n]s” as the first parameter in the function. Otherwise, you can get a string with space.

Then you need to define two arrays to store compress string and count of character. The first array use for storing character must be character type(char) array. Another one which we use store count is an integer type array. In here, I use the size of two arrays as “strlen(text)+1” because imagine the given text can not compress, then you need to store the same string.

Let me give an example for a better understanding.

Sample input-abcdef

Sample output-abcdef

So in this situation, you need to store the same array. Also, you need to remember if you store a string in an array, you need an additional memory location. It is used to store the null character(‘’). It indicates the end of a string. So you must use “strlen(text)+1” memory locations for temp and counts array.

In lines 10 and 11, we declare two variables. One is “count” it keeps track of repetitions. The second one is the “counter”.It is used to store the index of temp and counts arrays.

Now is time for the main part of the program.

In line 11, we use for loop to iterate over the text array. We use the strlen function to measure the size of the string, which store in the text array.

In line 13, we check it is the first element of the array or not. If it is the first element of the string, then we store that character in a temp array, and we set the count of that character(count variable) to 1.

In line 26, we check whether it is the last element of the array; if it is the last item of the array, we check whether if that element is equal to the previous element. If it is equal to the previous element, we increase the count and store it in counts array. Else we store the last element of the temp array and set count as 1 in counts array. We also increase the counter and store the null character at the end of temp to convert that array to string.

In line  18, we check whether the present element is not equal to the previous element. Then we store the previous element counter in the counts array and count to zero to measure the count of the present element. We increase count by one and increase the counter by one to move the next location of the temp and counts array. We store the present element of the array in temp.

In line 48, else only works when present element=previous element, and the present element is not the last or first element of the text array. Then we increase the count by one.

In line 54, we use for loop to iterate over temp and counts array and get the output. In here, if the character count is one, then we only print character; otherwise, we first print the character, then we print the count of it.

 

28
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x