top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: How to convert a string where each char is followed by its occurrence into absolute string?

+3 votes
645 views

I need help in writing the code where I enter the input in which each char is followed by its occurrence and output should be absolute string.

Example
Input a10b5c4
Output aaaaaaaaaabbbbbcccc

posted Dec 7, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    char input[100] = "", c, x;
    int i = 0, num = 0;

    printf("Enter the input (char followed by its occurrence)\n");
    scanf("%s", input);

    while ((c = input[i++])) {
        if ((c >= 'a' && c <='z') || (c >= 'A' && c <= 'Z')) {
            x = input[i];
            while(isdigit(x)) {
                num = (num * 10) + (x - '0');
                x = input[++i];
            }
            while(num--)
                printf("%c", c);
            num = 0;
       }
    }

    printf("\n");
    return 0;
}

Here is the expected output for given input

input: a10b5c4
output: aaaaaaaaaabbbbbcccc
answer Dec 10, 2015 by Arshad Khan
0 votes
#include<stdio.h>
int main()
{
    char inputstring[100],outputstring[1000];
    int i,j,temp;

    printf("Enter the string\n");
    scanf("%s",inputstring);

    i=1;
    j=0;

    char ch=inputstring[0];

    while(inputstring[i]!=NULL)
    {
        if(inputstring[i]>='0'&&inputstring[i]<='9')
        {
            temp=0;
            while(inputstring[i]>='0'&&inputstring[i]<='9')
            {
                temp=temp*10+inputstring[i]-48;
                i++;
            }
            while(temp--)
                outputstring[j++]=ch;
        }
        else
        {
            ch=inputstring[i];
            i++;
        }
    }

    outputstring[j]='\0';
    printf("\n%s",outputstring);

    return 0;
}
answer Dec 7, 2015 by Rajan Paswan
Similar Questions
0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

+1 vote

Consider an implementation of Strings consisting of an array where the first element of the array indicates the length of the String and the next elements represent the value of the ASCII characters in the String.
Implement String concatenation of such scenario using C?

...