My code gets a string of characters. For example "aaabbdddd" A function inserts to a new string the letters and the amount of times they appear. So the output for this specific string should be "a3b2d4". My question is how do I insert the numbers into the string? I tried using itoa and it converted the entire string into a single number. Here's my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LONG 80
#define SHORT 20
void longtext(char longtxt[LONG], char shorttxt[SHORT])
{
int i, j=0, count=0, tmp;
char letter;
for (i = 0; i <= strlen(longtxt); ++i)
{
if (i == 0)
{
letter = longtxt[i];
++count;
}
else if (letter == longtxt[i])
++count;
else
{
shorttxt[j] = letter;
shorttxt[j + 1] = count;
j += 2;
count = 1;
letter = longtxt[i];
}
}
}
int main()
{
char longtxt[LONG] = "aaabbdddd",shorttxt[SHORT];
longtext(longtxt,shorttxt);
printf("%s", shorttxt);
}
I believe that the problem is in the line "shorttxt[j + 1] = count;" because thats where I want to put the int into the string.
c++tag.