Friday 6 July 2012

Reduce aabcccccaaa to a2b1c5a3 for new string is of smaller size otherwise live it original




#include<stdio.h>
#include<string.h>
int main()
{
     char s[20],o[20],temp;
     int i=0,j=0,c=0;
     gets(s);
     while(s[i]!=NULL)
     {
         temp=s[i];
         while(s[i]== temp)
         {
             i++;
             c++;
         }
         o[j++]=temp;
         o[j++]=c+48;// ASCII of 0 is 48
         c=0;
     }// end of while
     if(strlen(o)<strlen(s))
        puts(o);
    else
        puts(s);
return 0;
}

No comments:

Post a Comment