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;
}

3 comments:

  1. #include
    #include
    using namespace std;
    int main()
    {
    char *str,letter;
    int i,n=0,num=0;
    str=(char *)malloc(40);
    cin>>str;
    while(str[n]!='\0')
    {
    n++;
    }
    str=(char *)realloc(str,n*sizeof(char));
    for(i=0;i9 )
    {
    letter=str[i];
    printf("%c",letter);
    }
    else if(str[i]-48>=0 && str[i]-48<=9)
    {
    num=str[i]-48;
    if(str[i+1]-48>=0 && str[i+1]-48<=9)
    {
    i++;
    num*=10;
    num+=(str[i]-48);
    }

    for(int j=1;j<num;j++)
    {
    cout<<letter;
    }
    }
    }
    }

    ReplyDelete
  2. #include
    #include
    #include
    using namespace std;

    int main ()
    {
    char s[10] = "b3c6d15", c;
    int i, j = 0, n = 0;
    for (i = 0; i < strlen (s); i++)
    {
    if(isalpha(s[i]))
    {
    c=s[i];
    if(isdigit(s[i+1]))
    {
    n=s[i+1]-48;
    if(isdigit(s[i+2])){
    n=(n*10)+(s[i+2]-48);
    }
    for(j=0;j<n;j++)
    {
    cout<<c;
    }
    }
    }

    }

    }

    ReplyDelete