Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:34:44+00:00 2026-06-16T13:34:44+00:00

I have a large string char myStr=AAAABBBCCCCCCDDDEFGHHIJJ. I shall pass this string to my

  • 0

I have a large string char myStr=”AAAABBBCCCCCCDDDEFGHHIJJ”.
I shall pass this string to my string compressing function which should return me the string in below format myStr =”A4B3C6D3EFGH2IJ2″
Also, the new string replacements should happen in the same passed string only. One cannot create a temp array.

Below is my func and am not able to figure out the deletion of duplicates and replacing with its count in the same string.

 #include<stdio.h>
 #include<string.h>


 char* StrCompress(char myStr[])
 {
char *s = myStr;
int len = strlen(myStr);
char *in = myStr;
int count =0;
int i=0;


while(*(s) != '\0')
{
    if(*(s)==*(s+1))
    {
        count++;

        if(count == 1)
        {
            in = s;
        }
        s++;

    }
    else
    {
        //myStr[count-1]=count;
        memcpy(in+1,s+1,count);
        s=in;
        count =0;

    }
    i++;
}

return myStr;



}

int main(){

char myStr[] ="AAAABBBCCCCCEEFGIIJJJKLMNNNNOOO";

printf("Compressed String is : %s\n",StrCompress(&myStr));

return 0;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:34:45+00:00Added an answer on June 16, 2026 at 1:34 pm

    A slightly modified version:

    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;
    }
    

    Additionally, you should not use the address of operator when calling with an array name:

    StrCompress(myStr); // not StrCompress(&myStr)
    

    If you are assuming that a character can’t repeat more then 9 times, then you can use in[0] = '0' + count instead of the sprintf stuff:

    if (count > 1) {
      in[0] = '0' + count;
      in++;
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large string, similar to this one: BREW pot HTCPCP/1.0 Accept-Additions: #milk;3#whiskey;splash
I have this string containing a large chunk of html and am trying to
I have a very large table in MySQL. I'm using a CHAR(32) field which
I have a string like this This:string:must~:be:split:when:previous:char:is:not~:this I need to split the line with
i have a large string (e.g. 20MB). i am now parsing this string. The
I have quite a large nvarchar which I wish to pass to the HashBytes
I have the below mentioned function in a large piece of code(in c++): void
I have a large string in a variable that includes a whole bunch of
I have a large string having dates, Ip addresses etc.. I need to split
I have a large string I need to parse, and I need to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.