0

What i want to do is make a String of 4 integers with whitespaces in between. Like in Java:

String res = num1 + " " + num2 + " " + num3 + " " + num4;

But I can't figure out how to do it in C.

int numWords = 0;
int numLines = 0;
int numChars = 0;
int numBytes = strlen(string);
char *result = malloc(sizeof(char) * 10);
result += numWords; //doesnt work is there somekind of function in c to do this?
joce
  • 9,624
  • 19
  • 56
  • 74

3 Answers3

4

You can use sprintf to convert each number to a string (and strcat to place them one after the other if necessary). You should keep track of the length of the string to ensure you don't overflow it.

For example:

int var = 10;
char buf[20];
sprintf(buf, "%d", var);  // buf string now holds the text 10

You don't need to make it much more complicated than this if you have a set format and amount of numbers. So if you always need one space between four numbers, you could do it all with one sprintf and a format string like "%d %d %d %d" (though this would require a much larger character array).


It'd be easy to write a small utility function that adds to an existing string, something like:

int add_to_string(char *buf, size_t sz, int num)
{
   char tmp[20];
   sprintf(tmp, " %d", num);

   size_t len = strlen(tmp) + strlen(buf) + 1; 
   if (len > sz)
      return -1;

   strcat(buf, tmp);
   return 0;
}

which you'd call with something like:

char buf[100];
sprintf(buf, "%d", 42);
add_to_string(buf, sizeof(buf), 9);
add_to_string(buf, sizeof(buf), 15);
add_to_string(buf, sizeof(buf), 8492);
add_to_string(buf, sizeof(buf), 35);
printf("String is '%s'\n", buf);

Output:
String is '42 9 15 8492 35'
teppic
  • 8,039
  • 2
  • 24
  • 37
1
#define FMT_STR "%d %d %d %d"

char *make_string(int num1, int num2, int num3, int num4)
{
    char *res;
    int len;

    // compute the length
    len = snprintf(NULL, 0, FMT_STR, num1, num2, num3, num4) + 1;
    // allocate and use the buffer
    res = malloc(len);
    snprintf(res, len, FMT_STR, num1, num2, num3, num4);

    return res;
}
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • 1
    You have a **horrible** bug. The length that `snprintf` returns does **not** include the required NULL terminator. In other words, you need to `malloc(len+1)` bytes. Please correct your code. And please don't cast the return from `malloc` in C, it's unnecessary and considered poor form. – Nik Bougalis Mar 13 '13 at 17:48
  • @NikBougalis Right, bug is fixed. What about "poor form"? I have not heard of it? Any link? – Valeri Atamaniouk Mar 13 '13 at 17:54
  • Check out: http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc – Nik Bougalis Mar 13 '13 at 17:56
  • 1
    @ValeriAtamaniouk: As of C89, the cast is no longer necessary, and under that version it can mask a bug if you forget to `#include` stdlib.h or otherwise don't have a declaration for `malloc` in scope. Since C99 got rid of implicit `int`, the bug isn't so much of an issue anymore, but it *is* unnecessary and just adds visual clutter. Note that C++ is different in this regard; C++ doesn't allow you to assign `void *` values to other pointer types without a cast. – John Bode Mar 13 '13 at 17:58
1

Use sprintf:

char target[SOME_SIZE];
...
sprintf(target, "%d %d %d %d", num1, num2, num3, num4);

You'll have to make sure your target is large enough to accommodate the string representations of all four numbers, plus three spaces, plus any signs, plus the 0 terminator. It takes 3.3 bits to represent a decimal digit, so a 32-bit int can have up to 10 decimal digits.

If you want to allocate the target buffer dynamically, you'd do something like

size_t maxdigits = (size_t) ceil((sizeof num1 * CHAR_BIT) / 3.3);
char *target = malloc( sizeof *target * ((4 * maxdigits) + 3 + 4 + 1)); 

+3 for the spaces, +4 for any sign characters (+/-), +1 for the 0 terminator.

John Bode
  • 119,563
  • 19
  • 122
  • 198