-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_length_encoding.cpp
More file actions
52 lines (45 loc) · 923 Bytes
/
run_length_encoding.cpp
File metadata and controls
52 lines (45 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
int t;
cout<<"Enter the no of test cases\n";
cin>>t;
while(t--){
string str,rle="";
stringstream out;
cout<<"Enter a string\n";
cin>>str;
int len = str.length();
int start = 0;
for(int i=1;i<len;i++){
if(str[start]!=str[i]){//start of new char sequence
out<<i-start;
string count = out.str();//converting int to string
// rle = rle + to_string(start); //in C++ 11
rle = rle + str[start] + count;
out.str("");//flushing stringstream
start = i;
}
}
out<<len-start;//last char count
string count = out.str();
rle = rle + str[start] + count;
cout<<"Run length encoding: "<<rle<<endl;
}
return 0;
}
/*
test cases:
3
Enter a string
aaaaaaaabbbbqqwooop
Run length encoding: a8b4q2w1o3p1
Enter a string
abc
Run length encoding: a1b1c1
Enter a string
qprqqqpp
Run length encoding: q1p1r1q3p2
*/