Home:ALL Converter>Append same string before every character in another string

Append same string before every character in another string

Ask Time:2016-09-19T14:30:43         Author:S.ai

Json Formatter

Can we Append same string before every character in another string in LoadRunner?

If I give input like:

char *s1 = "Hello";
char *s2 = "\\x";

I want to print in output like: "\xH\xe\xl\xl\xo"

Could you please help.

Author:S.ai,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39566680/append-same-string-before-every-character-in-another-string
Ryan Haining :

A series of strcpy()s gets what you need\n\nsize_t len1 = strlen(s1);\nsize_t len2 = strlen(s2);\n\nchar* res = malloc(len1 * (len2+1) + 1);\nres[0] = '\\0';\nsize_t pos = 0;\nfor (const char *p = s1; *p; ++p, pos += (len2+1)) {\n // append s2 to the string\n strcpy(&res[pos], s2);\n // append the next character\n strncpy(&res[pos+len2], p, 1);\n // add the NUL\n res[pos+len2+1] = '\\0';\n}\nputs(res);\n\n\nThis could be done with less code with strcat but that would require walking the string on each call.",
2016-09-20T18:56:52
yy