Wednesday 22 June 2016

Write a program to give the following output for the given input E.g.: Input: a1b10 Output: abbbbbbbbbb Input: b3c6d15 Output: bbbccccccddddddddddddddd The number varies from 1 to 99.

#include<stdio.h>
#include<stdlib.h>
void main()
{
    char str[100000];
    int count,i,len=0,j,num;
    scanf("%s",str);
    while(str[len++]);
    for(i=0;i<len;)
    {
        int x =(int) (str[i+1]-'0');
        int y =(int) (str[i+2]-'0');
        if((x<=9)&&(x>=1)&&(y<=9)&&(y>
            num= (x*10) +y;
        else
            num=x;
        if(num>=1&&num<=99)
        {
            for(j=0;j<num;j++)
            {
                printf("%c",str[i]);
            }
        }
        if(num>=1&&num<=9)
            i=i+2;
        else
            i=i+3;
    }
}

4 comments:

  1. I want this program as output a2b2c4a2 and input should be aabbccccaa

    ReplyDelete
    Replies
    1. private void checkRegex() {
      String regexPattern = "(?<=[0-9])(?=[a-zA-Z])";
      String regexPatternInner = "[^a-z]";
      String inputStr = "a1b10cc12";
      String[] splitStr = inputStr.split(regexPattern);

      Log.d("out", String.valueOf(splitStr.length));
      String output = "";

      for (int i = 0; i < splitStr.length; i++) {
      // regex to split
      String charString = splitStr[i].split(regexPatternInner)[0];
      int repeatCount = Integer.parseInt(splitStr[i].replaceAll("[^0-9]", ""));
      for (int j = 0; j < repeatCount; j++) {
      System.out.print(charString);
      output = output + charString;
      }
      Log.d("", output);
      }
      }

      Delete