Cocubes Coding Questions – 7
You’re given a function –
char *CompressString(char* str);
The function accepts a string as an argument that may contain repetitive characters. Implement the function to modify and return the input string, such that each character once, along with the count of consecutive occurrence. Do not append count if the character occurs only once.
Note –
- The string will only contain lowercase English Alphabets
- If you have to manipulate the input string in place you cant use another string
Assumption –
No character will occur consecutively more than 9 times.
Example –
input – aaaaabbbccccccccdaa
OutPut – a4b3c8da2
Please write the code in the comments in all languages it will be added here later –
Login/Signup to comment
C solution
char a;
int count,i=0;
int num=0;
char s[100];
scanf(“%s”,s);
int n=strlen(s);
while(i < n)
{
a=s[i];count=0;
for(int j=0; j<n; j++)
{
if(a == s[j])
{
count++;
}
}
printf("%c%d",a,count);
num+=count;
i=num;
}
s=input()
def count(s,q):
c=0
for i in s:
if i==q:
c+=1
if c>1:
print(c,end=”)
else:
pass
l=[]
for i in s:
if i not in l:
l.append(i)
print(i,end=”)
count(s,i)