how compress string in c

Solutions on MaxInterview for how compress string in c by the best coders in the world

showing results for - "how compress string in c"
Cliff
29 Jun 2017
1#include <bits/stdc++.h>
2using namespace std;
3class Solution {
4   public:
5   string solve(string s) {
6      string ret = "";
7      for(int i = 0; i < s.size(); i++){
8         if(ret.size() && ret.back() == s[i]){
9            continue;
10         }
11         ret += s[i];
12      }
13      return ret;
14   }
15};
16int main(){
17   Solution ob;
18   cout << (ob.solve("heeeeelllllllloooooo"));
19}
Ilaria
07 Sep 2019
1char* StrCompress(char myStr[])
2{
3  char *s, *in;
4  for (s = myStr, in = myStr; *s; s++) {
5    int count = 1;
6    in[0] = s[0]; in++;
7    while (s[0] == s[1]) {
8      count++;
9      s++;
10    }   
11    if (count > 1) {
12      int len = sprintf(in, "%d", count);
13      in += len;
14    }   
15  }
16  in[0] = 0;
17  return myStr;
18}
19