Bookmark and Share

free counters
Back to Problem list
Problem 25: 
Consider the stringAAAABCCCCCDDDDconsisting of alphabetic characters only. 
This string is of length 14. Since the string consists of alphabetic characters
only, duplicate characters can be removed and replaced with a duplication
factor n. With this technique the string can be compressed and represented 
by ‘4AB5C4D’.The compressed string is of length 7. Write a program which takes
a string in compressed form 
and recreates the original uncompressed string.

Note: The string will be of the formatnA...’ where n, the duplication factor, 
is an integer between 2 and 99 and A is an uppercase alphabetic character.

Input:3A4B7D
output:AAABBBBDDDDDDD
Input: 2BA3C
Output: BBACCC

#include<stdio.h>
#include<ctype.h>
#include<string.h>

char input[100];

void Process() {
    int i, j, pre = 0;
    for(i = 0; input[i]; ) {
        if(isalpha(input[i])) {
           if(pre == 0) pre = 1; // why ? consider input 2BA3C where 'A' is given once but 1 is not in the input
           for(j = 0; j<pre; j++) {
              printf("%c",input[i]);
           }
           i++;
           pre = 0;
           continue;
        }else if(isdigit(input[i])) {
           pre = pre*10 + input[i] - '0';
           i++;     
        }
    }
}
int main() {
    gets(input);
    Process();
    return 0;
}


Back to Problem list