Compress a string

Compress a string

Write a program to compress the given string 

Sample input : aabbbccccddddd
Sample output : a2b3c4d5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string a;
    int k=0,count=1,i=0,j,n;
    cout<<"Enter the string : ";
    cin>>a;
    n=a.size();
    cout<<"Compressed String : ";
    while(i<n)
    {
        
        for(j=k;j<n;j++)
        {
            if(a[j]==a[j+1])
            {
                count++;
                k=j+1;
            }
            else
            {
                k=j+1;
                break;
            }
        }
        i=k;
        cout<<a[i-1]<<count;
        count=1;
    }

    
}

OUTPUT:


0 comments