You are the encoded form of a data string as follows: consecutive
occurrences of a letter (up to 9) are represented by the letter
followed by the number of occurrences.
For example, the string
a9a3b2c4de
stands for the string
aaaaaaaaaaaabbccccdc
- that is, 12 consecutive occurrences of a, followed by 2 bs, and then
4 cs, followed by a d and finally c
Given an encoded form, you have to output the data string.
Input
-----
The encoded form of the string, made as per the following rules.
1. If a character occurs only once, then in the encoded string, it
appears as such (for example, 'd' in the above string.)
2. If the number of consecutive occurrences of the character is
between 2 and 9, then it is represented as the character followed
by the number of occurrences (e.g. aaaab is represented as a4b).
3. If the number of consecutive occurrences of a character is greater
than 9, then group 9 occurrences as per rule 2. Iterate the set of
rules on the remaining string.
Output
------
The original string, consisting only of characters whose
encoding was given as input.
What I have tried:
#include<stdio.h>
int main() {
char str[20], ch;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &str);
printf("\nEnter the character to be searched : ");
scanf("%c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch)
count++;
}
if (count == 0)
printf("\nCharacter '%c'is not present", ch);
else
printf("\nOccurence of character '%c' : %d", ch, count);
return (0);
}