Tuesday, 3 February 2015

write C++ program to Compress the string

 
#include<iostream>
int main()
{
  std::string s="aabbcccccdddddeeeeee";
  std::string output;
  char out[15]={'\0'};

  int len=s.length();

  int repeatedCharacter=1;

  int outcount=0;

  char c;



  for(int i=0; i<len; i++)

  {

    repeatedCharacter=1;

    c=s[i];



    while(c == s[i++])

    { 

      repeatedCharacter++;

    } 



    std::cout<<c;

    std::cout<<repeatedCharacter;



    out[outcount++]=(char)c;

    out[outcount++]='0'+repeatedCharacter;



  }



  std::cout<<"\nFinal: "<<out;

  return 0;




}