Hello. I need help with my assignment urgently. This is my assignment question and the follwoing is my code. It compiles but it doesnt give me the right output.

  1. Run length encoding.
    Example: if the input is “AAABBCCCDEEFFA” the output will be “A3B2C3DE2F2A”

  2. Input takes the form of alphabetic characters only

  3. Must read in the input from standard input.
  4. Can't use C++ string data type or vectors. Only use dynamic arrays implemented with pointer.

    include <iostream>

    include <cstdlib>

    include <cstring>

    using namespace std;

    int main()
    {
    char str[26];
    char *ptr;

    ptr = str;
    cout << "Enter a string: " << endl;
    cin >> ptr;
    
    char chCount;
    unsigned int temp = 0;
    //char curr;
    for (unsigned int i = 0; i < strlen(str); i++)
    {
        if(str[i] == chCount)
            temp++;
            else{
                temp = 1;
            }
    }
    cout << temp << endl;
    
    
    //cout << "Hello world!" << endl;
    return 0;
    

    }

You have three mistakes:

(a) Your output is outside of the loop, you need to output each time that you get a new letter.
(b) you don't output the letter just the number.
(b) you don't deal with the special case of temp=1, i.e. you don't need to output the number.

That should help .. let us know if that you are still stuck.

This is more or a C than a C++ problem. Read one character from stdin (cin), count the number of instances of that character from stdin until you get a new character. Add that code (cN) to an array of results. Start with new character with a count of 1 and reiterate. Continue until all characters are read.

It is still giving me the incorrect output.

This article has been dead for over six months. Start a new discussion instead.