Tuesday 6 June 2017

Write a program to give the following output for the given input.(Encoding)

Input:  aaaabcccc
Output: a4b1c4
Input:  bccdddeeee
Output: b1c2d3e4
#include<stdio.h>
#include<string.h>
int main()
{
     int n,count=1,i,j;
    char a[50],ch; 
    scanf("%s",a);
    ch=a[0];
    for(i=1;i<strlen(a);i++)
    {
         if(ch==a[i])
            ++count;
         else {
             printf("%c%d",ch,count);count=1;ch=a[i];}
        if(i==strlen(a)-1)
        printf("%c%d",ch,count);
    }
    return  0;
}

No comments:

Post a Comment