Task: Given a string, Find the run length encoded string for given string.
Example:
1
aaaabbbccc
Output:
a4b3c3
Here is the Code:
Program in C++:
- #include<iostream>
- #include<bits/stdc++.h>
- using namespace std;
- char *encode(char *src)
- {
- int c=0;
- for(int i=0;src[i]!='\0';i++)
- {
- if(c==0)
- {
- cout<<src[i];
- c++;
- }
- else if(src[i-1]==src[i])
- {
- c++;
- }
- else
- {
- cout<<c;
- c=1;
- cout<<src[i];
- }
- }
- cout<<c;
- return "\0";
- }
- int main() {
- int T;
- cin>>T;
- while(T--)
- {
- char str[10000];
- cin>>str;
- cout<<encode(str)<<endl;
- }
- return 0;
- }
Here is the Video:
Comments
Post a Comment