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

  • SEARCH
  • Home
  • 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 8229861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:56:32+00:00 2026-06-07T16:56:32+00:00

I wrote a program to compress a string using the counts of repeated characters.

  • 0

I wrote a program to compress a string using the counts of repeated characters. if the compressed string is longer than the original string, then we still return the original string. Below is my program:

void stringCompress(char* src) {
    char* original;
    original = src;
    char* rst;
    rst = src;

    int histogram[256];
    for (int i = 0; i < 256; i++) {
        histogram[i] = 0;
    }
    int length = 0;
    while (*src != NULL) {
        length++;
        src++;
    }
    src = original;
    int j = 0;

    for (int i = 0; i < length; i++) {

        histogram[(int) src[i]]++;
        if (histogram[(int) src[i]] == 1) {
            rst[j] = src[i];

            j++;

        }

    }
    rst[j] = '\0';

    char* final;

    rst = original;
    int index = 0;
    char buffer[33];

    for (int i = 0; i < j; i++) {

        final[index] = rst[i];

        stringstream number;
        number<<histogram[(int)rst[i]];
------->        //cout<<number.str()<<endl;
        char* temp = new char[number.str().length()+1];
        strcpy(temp, number.str().c_str());
        index++;
        cout<<temp<<endl;
        for(int k =0 ;k<number.str().length();k++)
        {
            final[index]=temp[k];
            index++;

        }

    }

    final[index] = '\0';
    src = original;

    if (index <= length) {
        for (int i = 0; i < index; i++)

            cout<<final[i];
    } else {
        cout << src << endl;
    }

}

But strange thing is that if I leave the cout sentence cout<<number.str()<<endl; there (the arrow points to the sentence), then the output is right. For example, aaaabcdaa outputs a6b1c1d1 and aabcd outputs aabcd. However if I comment out cout<<number.str()<<endl;, then nothing is generated. Any help is appreciated.

  • 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-07T16:56:36+00:00Added an answer on June 7, 2026 at 4:56 pm

    The variable final is uninitialized in your code. When I initialize it with a memory buffer, then your program prints the desired output whether the line you pointed to is commented out or not.

    Perhaps you meant to use buffer (which is unused) as memory for final, such as:

    final = buffer;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program for downloading an image from web using AsyncTask in service
I wrote a program to click on an application automatically at scheduled time using
I wrote a program in C++/OpenGL (using Dev-C++ compiler) for my calculus 2 class.
I wrote program to control iTunes by monitoring keystrokes from with pyHooks and then
I wrote a program using Tweepy that I can run on my home computer
I wrote a program that simulates soft bodies using springs. It looks nice but
I wrote a program for linux using libxml2 for html parsing. Although it does
I would like to compress a directory using 7-Zip and then download it to
I need to write a program to compress/decompress txt files using Huffman algotrithm I
I wrote a program in C to generate fractals using a formula, but it

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.