Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
872 views
in Online Assessments by Expert (110,880 points) | 872 views

1 Answer

0 like 0 dislike
Best answer

Problem Statement: The string contains multiple characters that are repeated consecutively. Now, write a program that will reduce the size of the string using the given mathematical logic.

Sample Test Case:

Input: abbcccddddeeeee

Output: a1b2c3d4e5

Program in C:

//Learnprogramo - programming made simple
#include<stdio.h>
int main() {
char str[100];
scanf("%[^\n]s",str);
int k=0, i, j, count = 0;
char str1[100];
for(i = 0; str[i] != '\0'; i++)
{
count = 0;
for(j = 0; j <= i; j++) 
{
if(str[i] == str[j]) 
{
count++;
}//if..
}//for...
if(count==1)
{
str1[k] = str[i];
k++;
}//if..
}//for..
for(i=0; i<k; i++) 
{
count = 0;
for(j=0; str[j]!='\0'; j++) 
{
if(str1[i]==str[j])
{
count++;
}//if..
}//for..
if(count==1) 
{
printf("%c",str1[i]);
}//if..
else
{
printf("%c%d",str1[i],count);
}//else..
}//for..
return 0;
}
by Expert (110,880 points)