Tuesday, 6 June 2017

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

Eg 1: Input: a1b10
       Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
          Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.
#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,j,count=0;
    char a[50],ch;
    scanf("%s",a);    
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]>='0'&&a[i]<='9')
        {
            count=(count*10)+(a[i]-'0');
        }
        else if(count>0)
        { count--;
            for(j=0;j<count;j++)
            {
                 printf("%c",ch);
            }count=0;
        }        
        if(a[i]>'9')
        {
            ch=a[i];printf("%c",a[i]);
        }
     //   printf("%d\n",i);
        if(i==(strlen(a)-1))
        {--count;
            for(j=0;j<count;j++)
            {
                printf("%c",ch);
            }
        }
    }
    
    return 0;    
}

1 comment:

  1. namespace ConsoleApp172
    {
    class Program
    {
    static void Main(string[] args)
    {
    string name = "a10b5c4d3";
    int len = name.Length;
    int count = 0;
    char ch = ' ';

    for(int i =0; i < len; i++)
    {
    if(name[i] >= '0' && name[i] <= '9')
    {
    count = (count * 10) + (name[i] - '0');
    }
    else if(count > 0)
    {
    for(int j =0; j < count; j++)
    {
    Console.Write(ch);
    }
    count = 0;
    }
    if(name[i] > '9')
    {
    ch = name[i];

    }
    if(i == len -1)
    {
    for(int j =0; j< count; j++)
    {
    Console.Write(ch);
    }
    }
    }

    Console.ReadKey();
    }
    }
    }

    ReplyDelete