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.

darthsidious
  • 2,851
  • 3
  • 19
  • 30
  • Assuming `count` is a **simple** digit, you may use `newStr[newCount] = static_cast('0' + count);`. – Jarod42 Jul 22 '14 at 07: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. – James Kanze Jul 22 '14 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). – James Kanze Jul 22 '14 at 10:35

2 Answers2

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.

Community
  • 1
  • 1
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
0

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

PankajM
  • 417
  • 2
  • 10
  • 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. – James Kanze Jul 22 '14 at 10:30