1

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.

2
  • Please remove c++ tag. Commented Dec 3, 2015 at 7:42
  • did you try my answer below? Commented Dec 3, 2015 at 8:26

2 Answers 2

3

You are right, the problem is the line:

shorttxt[j + 1] = count;

Change it to:

shorttxt[j + 1] = count + '0';

And you should be fine.

The reason is that you don't want the number itself in the string, but the character representing the number. Adding the ascii value for the character 0 to the actual number gives you the correct result.

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

4 Comments

Thank you! it works! now i just need to find out how to get rid of al the gibberish after the wanted output :D
Great! Here's a hint for the gibberish part: Strings in C are zero-terminated.
but i defined that i <= strlen(longtxt). the output is the wanted line,gibberish and the string "longtxt" itself. i only want shorttxt.
shorttxt isn't initialized so it's 20 chars of garbage. You replace some of them with your output, the rest is still garbage. Here's an introduction to C strings: eskimo.com/~scs/cclass/notes/sx8.html
0

Try this code, it uses snprintf to convert integer to string.

NOTE: You may need to increase the size from 2, if count exceeds a character size.

#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;
            snprintf(&shorttxt[j + 1],2,"%d",count);
            j += 2;
            count = 1;
            letter = longtxt[i];
        }
    }
}
int main()
{
    char longtxt[LONG] = "aaabbdddd",shorttxt[SHORT];
    longtext(longtxt,shorttxt);
    printf("%s", shorttxt);
}

2 Comments

tried it, but it crashes the program. when i tried debugging it line after line it encountered an error in the loop.
@user5633902 I don't uderstand why it crashes. But check this link: ideone.com/YuN2qu There it's giving proper output... hmm

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.