Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcat() for formatted strings

I'm building a string piece by piece in my program and am currently using a mix of strcat() when I'm adding a simple string onto the end, but when im adding a formatted string I'm using sprintf() e.g.:

int one = 1;
sprintf(instruction + strlen(instruction), " number %d", one);

is it possible to concatenate formatted string using strcat() or what is the preferred method for this?

like image 709
BradStevenson Avatar asked Dec 24 '12 15:12

BradStevenson


People also ask

What does strcat () string function returns?

The strcat() function returns a pointer to the concatenated string (string1).

What is the purpose of strcat () function in C?

The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.

What is the difference between strcat and strncat?

The strcat() function appends the entire second string to the first, whereas strncat() appends only the specified number of characters in the second string to the first.

What is the purpose of strcat () function to cut a part of string to add two string one after other to create a duplicate string to copy string in another address?

In the C Programming Language, the strcat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1. It returns a pointer to s1 where the resulting concatenated string resides.


3 Answers

Your solution will work. Calling strlen is a bit awkward (particularly if the string gets quite long). sprintf() will return the length you have used [strcat won't], so one thing you can do is something like this:

 char str[MAX_SIZE];
 char *target = str;

 target += sprintf(target, "%s", str_value);
 target += sprintf(target, "somestuff %d", number);
 if (something)
 {
    target += sprintf(target, "%s", str_value2);
 }
 else
 {
    target += sprintf(target, "%08x", num2);
 }

I'm not sure strcat is much more efficient than sprintf() is when used in this way.

Edit: should write smaller examples...

like image 53
Mats Petersson Avatar answered Oct 13 '22 11:10

Mats Petersson


no it's not possible but you could use sprintf() on those simple strings and avoid calling strlen() every time:

len = 0;
len += sprintf(buf+len, "%s", str);    
len += sprintf(buf+len, " number %d", one);
like image 10
iabdalkader Avatar answered Oct 13 '22 12:10

iabdalkader


To answer the direct question, sure, it's possible to use strcat to append formatted strings. You just have to build the formatted string first, and then you can use strcat to append it:

#include <stdio.h>
#include <string.h>
int main(void) {
    char s[100];
    char s1[20];
    char s2[30];
    int n = 42;
    double x = 22.0/7.0;

    strcpy(s, "n = ");
    sprintf(s1, "%d", n);
    strcat(s, s1);

    strcat(s, ", x = ");
    sprintf(s2, "%.6f", x);
    strcat(s, s2);

    puts(s);
    return 0;
}

Output:

n = 42, x = 3.142857

But this is not a particularly good approach.

sprintf works just as well writing to the end of an existing string. See Mats's answer and mux's answer for examples. The individual arrays used to hold individual fields are not necessary, at least not in this case.

And since this code doesn't keep track of the end of the string, the performance is likely to be poor. strcat(s1, s2) first has to scan s1 to find the terminating '\0', and then copy the contents of s2 into it. The other answers avoid this by advancing an index or a pointer to keep track of the end of the string without having to recompute it.

Also, the code makes no effort to avoid buffer overruns. strncat() can do this, but it just truncates the string; it doesn't tell you that it was truncated. snprintf() is a good choice; it returns the number of characters that it would have written if enough space were available. If this exceeds the size you specify, then the string was truncated.

/* other declarations as above */
size_t count;
count = snprintf(s, sizeof s, "n = %d, x = %.6f", n, x);
if (count > sizeof s) {
    /* the string was truncated */
}

And to append multiple strings (say, if some are appended conditionally or repeatedly), you can use the methods in the other answers to keep track of the end of the target string.

So yes, it's possible to append formatted strings with strcat(). It's just not likely to be a good idea.

like image 2
Keith Thompson Avatar answered Oct 13 '22 12:10

Keith Thompson