Answers for "how to comper string in c"

1

how compress string in c

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   string solve(string s) {
      string ret = "";
      for(int i = 0; i < s.size(); i++){
         if(ret.size() && ret.back() == s[i]){
            continue;
         }
         ret += s[i];
      }
      return ret;
   }
};
int main(){
   Solution ob;
   cout << (ob.solve("heeeeelllllllloooooo"));
}
Posted by: Guest on January-11-2021
0

how compress string in c

char* StrCompress(char myStr[])
{
  char *s, *in;
  for (s = myStr, in = myStr; *s; s++) {
    int count = 1;
    in[0] = s[0]; in++;
    while (s[0] == s[1]) {
      count++;
      s++;
    }   
    if (count > 1) {
      int len = sprintf(in, "%d", count);
      in += len;
    }   
  }
  in[0] = 0;
  return myStr;
}
Posted by: Guest on January-11-2021

Browse Popular Code Answers by Language