Thursday, 14 February 2019

Expand Alphabets

A string S is passed as input. S will contain multiple integer values with each integer value followed by an alphabet. The program must expand the alphabets based on the related integer value.

Input Format:
The first line contains S.

Output Format:
The first line contains the expanded string value.

Boundary Conditions:
Length of S is from 2 to 100.

Example Input/Output 1:
Input:
4a5h
Output:
aaaahhhhh
Explanation:
As it is 4a and 5h, four a's are printed followed by 5 h's

Example Input/Output 2:
Input:
1k2b4k
Output:
kbbkkkk

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

int main()
{
   char S[100],num[100];
   int i=0,j=0,cnt=0;

   scanf("%s",S);
   while(S[i]!='\0')
   {
       
       if (isdigit(S[i]))
       {
          j=0;
          while(S[i]>='0' && S[i]<='9')
            num[j++]=S[i++];
            num[j]='\0';
            cnt=atoi(num);
        }  
        
            while (cnt>0)
            {
               printf("%c",S[i]);
               cnt--;
            }  
            ++i;
   }  
}

OUTPUT
100q2w
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqww

5 comments:

  1. Need answers for these kinda questions in all languages.

    ReplyDelete
    Replies
    1. Happy to see your comment. You can see my latest post "Python program to expand alphabet"

      Delete
  2. t='13e14r'
    num=[]
    for i in range(len(t)):
    if t[i].isalpha()!=True:
    num.append(t[i])
    else:
    num=[str(x) for x in num]
    nu = "". join(num)
    n = int(nu)
    for ii in range(n):
    print(t[i],end='')
    num=list()

    ReplyDelete