0

I wrote the following code to convert string of type 'aaadddbbbccc' to 'a3d3b3c3' :

#include <iostream>
#include <string.h>
using namespace std;

    void stringCompression(char *str,char *newStr){
        int a[256] = {0};
        int newCount = 0;
        for(int i = 0; i < strlen(str) ; i++){
            int j = str[i];
            if (a[j] == 0 && strlen(newStr) <= strlen(str)){
                a[j] = 1 ;
                newStr[newCount] = str[i];
                newCount++;
                int count = 0;
                for (int n = i; n < strlen(str); n++){
                    if(str[i] == str[n]){
                        count = count + 1;
                    }
                }
                newStr[newCount] =(char) count;
                newCount++ ;
            } else if (strlen(newStr) > strlen(str)){
                strcpy(newStr,str);
            }
        }
    }

    int main() {
        char str[] = "abcdabcdabcd";
        char *newStr = new char[strlen(str)+1];
        stringCompression(str,newStr);
        cout << newStr;
        return 0;
    }

My problem is at step

newStr[newCount] =(char) count;

even though it is inserted but the output is not a3b3c3d3 but a*squarebox*b*squarebox*c*squarebox*d*squarebox*. squarebox being 2*2 matrix with one value as the number that is desired. I am using eclipse IDE. . I would really appreciate your help. How can I correct this. Am I using the correct approach?

Thanks in advance.

3
  • Assuming count is a simple digit, you may use newStr[newCount] = static_cast<char>('0' + count);. Commented Jul 22, 2014 at 7:42
  • Not related to the problem, but you really don't want to use strlen in the condition of the loop. C style strings are designed for access through the pointer, so your outer level loop should be something like for ( char const* p = str; *p != '\0'; ++ p ), with *p to access the characters. Commented Jul 22, 2014 at 10:34
  • And of course, using the char converted to an int is not going to work for all characters, at least not if char is signed (which is often the case). Commented Jul 22, 2014 at 10:35

2 Answers 2

1

The problem is that

newStr[newCount] =(char) count;

converts the number "count" into the character corresponding to that number according to the ascii table (http://www.asciitable.com/), which is "end of text" for "3", that does not correspond to any number.

You should convert "count" into a string instead. See here for example: Easiest way to convert int to string in C++

However, be aware that it might be longer than one digit, for example if count is "11", it will take two letters in string representation.

Sign up to request clarification or add additional context in comments.

Comments

0

Hey you have to use http://www.cplusplus.com/reference/cstdlib/itoa/ to convert integer to char string

1 Comment

Not a very reliable reference, but they do point out that atoi is a sometimes thing---it's not standard, and not all compilers support it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.