Explanation ؟ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Explanation ؟

#include <iostream> using namespace std; #include <string> void compressString(char *sr, int n ) { int count1=1; for(int i=0; i<n-1; i++) { if(sr[i] == sr[i+1]) count1++; else { cout<<sr[i]<<count1; count1=1; } } } int main(void) { int n; cin>>n; char str[n+1]; cin>>str; compressString(str, sizeof(str)); return 0; }

9th Mar 2018, 11:07 PM
muhamedetish
13 Answers
+ 2
Ok, focus on smaller stuff first, that's just natural.
11th Mar 2018, 5:25 PM
Timon Paßlick
+ 6
look at the first thing in the method. it is a comparison statement which sees is the character (sr[i]) is the same as the next. (sr[i+1]) If it is, it increments the count1 variable. If not, it outputs that character (as it is unique to the one before it), and resets the count1 variable.
9th Mar 2018, 11:48 PM
J.G.
J.G. - avatar
+ 3
Ok, you always get a pair of char and int from the input. You call the string fill constructor with these pairs and push the result into the output.
10th Mar 2018, 1:36 PM
Timon Paßlick
+ 1
Can be explained more؟
10th Mar 2018, 3:09 AM
muhamedetish
+ 1
//This is how I'd write it without changing it too much. #include <iostream> using namespace std; #include <string> void compressString(const string& sr) { if (sr.empty()) { cout << "Empty input."; exit(0); } int count = 1; for (int i = 0; i != sr.length() - 1; i++) { if (sr[i] == sr[i + 1]) count++; else { cout << sr[i] << count; count = 1; } } cout << sr.back() << count; } int main(void) { string str; cin >> str; compressString(str); }
10th Mar 2018, 8:43 AM
Timon Paßlick
+ 1
You can write one and I give you feedback.
10th Mar 2018, 1:16 PM
Timon Paßlick
+ 1
What do you mean?
10th Mar 2018, 1:23 PM
Timon Paßlick
0
This is actually not the best code. But what it does is it always counts how big the sequence of a char is and outputs the char and its sequence count. You have to enter the size of the string first because of the codes poor use of the standard library. And the last char doesn't get processed.
10th Mar 2018, 8:30 AM
Timon Paßlick
0
can you write one "decompress"??
10th Mar 2018, 1:15 PM
muhamedetish
0
can you write one "decompress"??
10th Mar 2018, 1:15 PM
muhamedetish
0
oky ?
10th Mar 2018, 1:21 PM
muhamedetish
0
can you give me feedback? i can't write.
10th Mar 2018, 1:31 PM
muhamedetish
0
i try and i don't know 😓
11th Mar 2018, 3:35 PM
muhamedetish