nptel question 2

You are the encoded form of a data string as follows: consecutive
occurrences of a letter (up to 9) are represented by the letter
followed by the number of occurrences.

For example, the string


stands for the string


  • that is, 12 consecutive occurrences of a, followed by 2 bs, and then
    4 cs, followed by a d and finally c

Given an encoded form, you have to output the data string.


The encoded form of the string, made as per the following rules.

  1. If a character occurs only once, then in the encoded string, it
    appears as such (for example, ā€˜dā€™ in the above string.)
  2. If the number of consecutive occurrences of the character is
    between 2 and 9, then it is represented as the character followed
    by the number of occurrences (e.g. aaaab is represented as a4b).
  3. If the number of consecutive occurrences of a character is greater
    than 9, then group 9 occurrences as per rule 2. Iterate the set of
    rules on the remaining string.


The original string, consisting only of characters whose
encoding was given as input.

#include <stdio.h>
#include <stdlib.h>

int main()
int c;
int temp,i;
c = getchar();

while ( c != EOF && c !='\n' )

    if(c>=97 && c<=122)
     else if(c>=50 && c<=57)

    c = getchar();


return 0;