views:

90

answers:

5

I'm stuck at yet another C problem. How can I concatenate two strings with the second string being inserted before the first string?

This is what I came up with. Unfortunately I'm stuck at all these pointer to chars, char arrays et cetera.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[] )
{
  char* output;
  int i;
  for(i = 9; i > 0; i--)
  {
    unsigned int value = (unsigned int)i;
    char buffer[20];
    sprintf(buffer, "%u", value);
//    strcat(ouput, buffer);  // append before the string.
    // first loop: 9
    // second loop: 89
    // third loop: 789
  }

  printf("%s", output);
}

How must I correct my code to make it work? I guess I have to somehow set the output variable to empty. When do I need fixed widths for the char array or the pointer? 20 was just a random guess from me.

+2  A: 

I'm very confused, as your posted code has absolutely nothing to do with the problem you state. (Well, they both use strings, but that's about it)

  char* src  = "Hello, ";
  char* dest = "World!";

  char* temp;
  temp = malloc(strlen(src) +strlen(dest) + 1);
  strcpy(temp, front);
  strcat(temp, dest);

  dest = temp;

Unless dest is a fixed buffer of adequate size for the combined string. If so, then replace the last line with:

  strcpy(dest, temp);
  free(temp);

Now, if you want to specifically build the list of digits backwards, let's try a different tack:

  char buffer[10];
  buffer[9] = '\0';  // null terminate our string.
  char* output;    
  int i;    
  for(i = 9; i > 0; i--)    
  {    
       // this is a fast way of saying, sprintf("%u", i); 
       // works only for single digits
       char d = (char)('0' + i); 

       buffer[i-1] = d;
       output = &buffer[i-1];
       printf("%s", output);
   }
James Curran
+1  A: 

You can append the integer at the end of the string as:

int i;
char buffer[20];
for(i = 0; i < 10; i++) {
        sprintf(buffer+i, "%u", i);
}
printf("%s", buffer); // prints 0123456789
codaddict
um.. You can append *digits* at the end of the end like that. TO do a number > 9, you'd need to figure out the width.
James Curran
+2  A: 

Usually, you should just avoid the situation to start with. The most obvious solution for your example would be to simply count upward to start with. When that's not suitable, a recursive solution to reverse the order in which the string is built can still allow you to generate the string from beginning to end:

int build_string(int value, char *string) { 
    char temp[10];

    if (value > -1)
        build_string(value-1, string);

    sprintf(temp, "%d", value); // use snprintf if available.
    strcat(string, temp);
    return string;
}

int main() { 
    char result[20] = {0};
    build_string(9, result);
    printf("%s", result);
    return 0;
}
Jerry Coffin
+1  A: 

For your stated problem (insert one string in front of another), this code will do the job - but has no error checking. It assumes there is enough space in the target buffer for the existing string and the new prefix:

/* Insert string t in front of string s in string s */
char *strinsert(char *s, const char *t)
{
    char           *p = s + strlen(s);
    char           *q = p + strlen(t);
    char           *r = s;

    while (p >= s)
        *q-- = *p--;
    while (*t)
        *s++ = *t++;
    return(r);
}

What it does is copy the existing string up by the correct number of places so that there is space for the new string at the beginning.

Jonathan Leffler
+1  A: 

Assuming that the destination buffer is big enough and that the source and destination do not overlap:

// not sure what order to put the params - the usual C way is destination
// followed by source, but it's also potentially confusing that the result of 
// prepend(foo,bar) is "<bar><foo>".
char* prepend(char *restrict dest, const char *restrict src) {
    size_t len = strlen(src);
    memmove(dest + len, dest, strlen(dest));
    return memcpy(dest, src, len);
}

If the buffers may overlap (for example, if src is the second half of dest), this approach doesn't work.

If the destination buffer is not big enough, then someone has to allocate new memory for the result, in which case the question of which is the "source" and which the "destination" disappears - they're both "source" and neither is "destination".

Steve Jessop