Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C
#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);
}
Posted
Updated 16-Sep-17 22:02pm
v2
Comments
Patrice T 17-Sep-17 3:04am    
This code is for another problem.

Read your homework question again.
You need to read in a string, and then process it as pairs of characters.
The first character is the char to repeat, the second is the number of time to repeat it (N).

All you have to do is print the char N times, then move onto the next pair.

But this is your homework, so I'll give you no code!
 
Share this answer
 
plz help..where am i wrong?

#include<stdio.h>
int main()
{
int i,n,s;
char a[100];
scanf("%d",&s);
for(i=0;i<s;i++)
{
scanf("%c",&a[i]);
}
for(i=0;i<s;i++)
{
if((a[i]>='a' && a[i]<='z')&&(a[i+1]>='a'&& a[i+1]<='z'))
{

printf("%c",a[i]);
}

else if((a[i]>='a' && a[i]<='z') &&(a[i+1]>=2 && a[i+1]<=9))
{

n=a[i+1];
while(n>0)
{
printf("%c",a[i]);
n=n-1;
}

}
}
if(a[s-1]>='a'&& a[s-1]<='z')
{
printf("%c",a[s-1]);
}
return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 17-Sep-17 3:09am    
else if((a[i]>='a' && a[i]<='z') &&(a[i+1]>=2 && a[i+1]<=9))
You need to put quotes round the 2 and the 9, they are input as characters not integers.
Nuruddin Warsi 17-Sep-17 3:19am    
did it...still not working
Patrice T 17-Sep-17 3:12am    
Is it a solution to the question?
or you have many accounts?
Nuruddin Warsi 17-Sep-17 3:16am    
different person...was searching for a solution...came across this site and decided to post a question regarding the code
Patrice T 17-Sep-17 3:22am    
Open a new question for your problem
with the code ans explanation of what is wrong.
then delete this "solution"
According to the requirements
a9a3b2c4de

should expand to
aaaaaaaaaaaabbccccde

instead of
aaaaaaaaaaaabbccccdc


That said, expansion it is really a simple matter:
(error handling, i.e. incorrect input string format, left as exercise)
  1. read next character from encoded string, if there isn't a next character (end of the string reached) then exit
  2. if the read character is a letter then add it to the output string and record it into a variable (say cur_char) go back to step 1.
  3. the read character is a digit, convert it into the corresponding number, say d and add cur_char d-times to the output string
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900