Tuesday, 6 June 2017

Write a program to give the following output for the given input.(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;
}

21 comments:

  1. Can u give the program explanation

    ReplyDelete
  2. 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);
    }
    }

    ReplyDelete
  3. #include
    int main()
    {
    int num;
    char a;
    while(scanf("%c %d",&a,&num)==2)
    {
    while(num--)
    {
    printf("%c",a);
    }
    }
    return 0;
    }

    ReplyDelete
    Replies
    1. how to terminate the loop?

      Delete
    2. why the first while loop is checked whether its equal to 2 or not?

      Delete
    3. we are getting two input that's why we used 2

      Delete
    4. @Dhivya,you are amazing ya,fantastic logic👍

      Delete
    5. What a logic��

      Delete
    6. Here, they asked for string ...not for character repetition?

      Delete
    7. your code is nice but suits for particular conditions like s5g7f1 not for fgh2 or gh7

      Delete
  4. import java.util.*;
    public class RepeatCharacter {
    public static void main(String[] args) {
    String GivenString = "a2b5c23";
    List CharacterList = new ArrayList<>(Arrays.asList(GivenString.split("[0-99]")));
    List NumberList = new ArrayList<>(Arrays.asList(GivenString.split("[a-z]")));
    CharacterList.removeIf(Character->(Character.isEmpty()));
    NumberList.removeIf(Number->(Number.isEmpty()));
    RepeatCharacter obj = new RepeatCharacter();
    obj.printChar(CharacterList, NumberList);
    }
    void printChar(List CharacterList, List NumberList) {
    for (int i = 0; i < CharacterList.size(); i++) {
    for (int j = 0; j < Integer.valueOf(NumberList.get(i)); j++) {
    System.out.print(CharacterList.get(i));
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  5. Program explanation please!

    ReplyDelete
  6. class Solution{
    static String input="a1b10";
    static char tempC;
    static int tempI,count=0,tempT;

    public static void main(String[] args) {

    StringBuilder in=new StringBuilder(input+"z");

    for(int i=0;i=97&&in.charAt(i)<=122) {
    tempC=in.charAt(i);
    }else {
    for(int j=i;j122) {
    tempT=j;
    }else {
    tempI=Integer.parseInt(in.substring(i,j));
    break;
    }
    }
    for(int k=0;k<tempI;++k) {
    System.out.print(tempC);
    }
    count=0;
    i=tempT;
    }
    }

    }
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete


  8. //if someone is looking for the code in cpp
    //headerfiles --> [iostream , ctype.h , string.h]
    #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;
    }
    }
    }

    }

    }
    //hope this helps

    ReplyDelete
  9. C code:
    #include
    #include
    int main()
    {
    char s[100];
    int i,j,count=0,count1=0;
    gets(s);
    for(i=0;i='1'&&s[i]<='9')
    {
    if(s[i+1]>='0'&&s[i+1]<='9')
    {
    count=10*(s[i]-'0');
    count1=s[i+1]-'0'+count;
    for(j=0;j<count1;j++)
    {
    printf("%c",s[i-1]);
    }
    i++;
    }
    else
    {
    count=s[i]-'0';
    for(j=0;j<count;j++)
    {
    printf("%c",s[i-1]);
    }

    }
    }
    else
    {
    printf("%c",s[i]);
    }
    }

    }

    ReplyDelete
  10. #include

    int main()
    {
    char ch[100],ch1[100];
    int c=0,j=0;
    scanf("%s",ch);
    for(int i=0;ch[i]!='\0';i++){
    if(isalpha(ch[i])){
    ch1[j]=ch[i];
    j++;
    }
    if(isdigit(ch[i])){
    c=c*10+(ch[i]-48);
    }
    if(!isdigit(ch[i+1])){
    for(int k=0;k<c;k++){
    printf("%c",ch1[j-1]);
    }
    c=0;
    }
    }

    ReplyDelete

Home Page

 Zoho Interview Questions Alternate Sorting   C program for Circular Pattern C program for Spiral Matrix C program to print ...