4

i use strcat() to connect two strings like:

    #include <string.h>
    #include <stdio.h>

    int main(int argc, char *args[])
    {
       char *str1; // "456"
       char *str2; // "123"

       strcat(str1,str2);
       printf("%s",str1);
    }

i get:

456123  

but i need the second string on beginning of first string like:

123456

how can i do it ?

Refael
  • 6,753
  • 9
  • 35
  • 54
  • 1
    You example is not clear. What is `char * str1; // "456"` meant to do? It won't produces the output you show, but run into Undefined Behaviuor, as there is no memory allocated for any characters, but `str1` and `str2` point just somewhere, randomly. – alk May 26 '13 at 13:30
  • str1 is a long string, I need to add another string at the beginning. – Refael May 26 '13 at 13:36

7 Answers7

7

Do strcat(str2,str1);, switch the parameters. But you will get resultant string in str2, which you can set to str1 if you really want to use str1 further in your program.

However, you need to take care appropriately for memory space available in str2.


If you want to change str1 then, do this

char *tmp = strdup(str1);

strcpy(str1, str2); //Put str2 or anyother string that you want at the begining
strcat(str1, tmp);  //concatenate previous str1

...
free(tmp); //free the memory
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • i need to add " " (blank) to beginning of str1 – Refael May 26 '13 at 13:38
  • @Refael, Check updated answer. You can change it to append as many strings possible. – Rohan May 26 '13 at 13:47
  • Ooh, this asumes `str1` referencing enough memory! Which is not clear from you example. `strcat()` is nothing more then a pseudo smart copy function. It's surely no "memory-area-concatenator"! It does **not** append the memory allocated by the call to `strdup()` to the memory referenced by `str1`! Only `realloc()` is able to extend an area of already allocated memory. – alk May 26 '13 at 13:48
  • @alk, this assumes that `str1` has enough memory. – Rohan May 26 '13 at 13:53
2

Try sprintf() to print the new value to the first string

sprintf(str1,"%s%s",str2,str1);
adazem009
  • 121
  • 1
  • 2
1

try to use this with static sized arrays, works for me in my project.

void strbeg(char* strReceive, char* strInsert)
{
    int strInsertLength = strlen(strInsert);
    int strReceiveLength = strlen(strReceive);

    for(int i = strReceiveLength; i >= 0; i--)
    {
        strReceive[i + strInsertLength] = strReceive[i];
    }

    for(int i = 0; i < strInsertLength; i++)
    {
        strReceive[i] = strInsert[i];
    }
}
Sornii
  • 421
  • 3
  • 11
0

You need to use strcpy instead of strcat.

unxnut
  • 8,509
  • 3
  • 27
  • 41
0

Verbatim from man strcat:

char *strcat(char *dest, const char *src);

DESCRIPTION

The strcat() function appends the src string to the dest string, overwriting the null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.

As the programmer you need to make sure that the pointer char * dest references enough valid memory to hold the addtional chars that will be copied from where char* src points to.

To succesfully prefix str1 with str2 declare them as follows:

char str2[3 + 1] = "123"; // three for "123" plus 1 for the terminating 0
char str1[2 + 3 + 1] = "46"; // 2 for "46" plus 3 for "123" plus 1 for the terminating zero

to actually concatenate the two char arrays do so:

strcat(str1, str2);
alk
  • 69,737
  • 10
  • 105
  • 255
0

If you segfault on this because you have two non-allocated strings with different sizes, maybe it'll solve the probleme. I first calculated the size difference to allocate the first string with the bigger size (in which [padding] will be concatenated at the begining). Then added the code of @Rohan.

if (len1 < len2) {
    char *tmp = strdup(num1);
    num1 = (char *) malloc(sizeof(char) * (strlen(num1) + diff));
    strcpy(num1, padding);
    strcat(num1, tmp);
    printf("%s\n", num1);
    free(tmp);
}
Hexagon
  • 1
  • 2
0

If you only want to add a single char :

char *add_char_to_beginning(char c, char **str)
{
    char *tmp = strdup(*str);
    *str = (char *) realloc(*str, sizeof(char) * (strlen(*str) + 2));
    *(str[0]) = c;
    strcpy(*str + 1, tmp);
    free(tmp);

    return *str;
}
Hexagon
  • 1
  • 2