Create a string , given characters and frequency of each character to repeat consecutively

For Example:

Input: a2b12

Output: aabbbbbbbbbbbb

Input: b4c7d11e1

Output: bbbbcccccccddddddddddde

The number ranges from 1 to 99.



Implementation:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
 
 /* Pointer to the array of characters */
  char *s;
  char c;
  int i,j,n,flag=0;
  
  /* Allocate memory block of 256 characters and return the starting
  address of the block to character pointer 'c' */
  s=(char*)malloc(sizeof(char)*256);
  
  /* Get the input string from the user */
  scanf("%s",s);
  
  i=0;
  
  /* Until we reach the end of the string (null character ('\0')) */
  while(*(s+i))
  { 
   /* Set the flag value as zero for every character */
    flag=0;
    
    /* store every alphabet we encounter while we are traversing the string */
    c=*(s+i);
    
    /* Store the digit or number next to the alphabet */
    n=*(s+i+1)-'0';
    
    /* Check if the number has two digits,
    if so, consider two digit number */
    if(!(*(s+i+2)>='a' && *(s+i+2)<='z'))
    {
     
     /* move the first digit to ten's place to take up
     new number in unit's place */
      n=n*10;
      
      /* convert the character to number and add it to unit place */
      n+=(*(s+i+2)-'0');
      
      /* Mark that the number is two-digit number */
      flag=1;
      i=i+3;
    }
    
    /* If the number is single-digit */
    if(flag==0)
     i=i+2;
    
    /* Print the characters for 'n' number of times */
    for(j=0;j<n;j++)
     printf("%c",c);
  }
  return 0;
}


Input:

a2b12

Output:

aabbbbbbbbbbbb