0

I want to create a big string 'des' from 2 substrings copied from string 'scr' this way :

I want to copy the substring, lets call it - 'string1' ( from ptr x to the end of the 'scr' string), then to place it in the 'des' and after that to concatenate it with another substring, lets call it - 'string 2' copied from the 'scr' string from the head of the string to x ptr. How can I do it not using a temp string ?

for example : scr = "ThisIs", string1 = "Is", string2 = "This" des = "IsThis"

I don't want to use a temp string to hold string 1 or string2.

Can you help me?

2
  • Is des pointer equal to scr pointer? Commented Apr 17, 2016 at 7:14
  • maybe you can use memcpy() . it would help you . but be sure that "des" has enough space to hold whole content Commented Apr 17, 2016 at 7:14

2 Answers 2

1

You don't need a temp string, you only need a pointer to hold the boundary of substrings. Try following:

char src[] = "ThisIs";
char dst[7] = {'\0'};
int len = strlen(src);
int str1len = 4;
strncpy(dst, src + str1len, len - str1len);
strncpy(dst + len - str1len, src, str1len);
printf("src=%s, dst=%s\n", src, dst);
Sign up to request clarification or add additional context in comments.

Comments

1

If you know the position of the second string, you can just print the two substrings to the destination string in reverse order:

char *src = "ThisIs";       // source string
char dst[7];                // char buffer for destination string

int pos = 4;                // position of second substring

snprintf(dst, sizeof(dst), "%s%.*s", src + pos, pos, src);
puts(dst);

Explanation:

  • snprintf writes formatted data to a string, just as printf writes formatted data to the screen. It takes the buffer length as second argument and uses it to ensure that the buffer will not overflow. It also guarantees that the resulting string is null terminated.
  • If the output would be a string with more characters than the buffer can hold, the output is truncated. snprintf returns the length that the string would have if the buffer were ininitely large. You can use that return value to check whether the output was truncated.
  • The second substring is null-terminated, because it ends where the whole string src ends. You can print it with printf("%s", str + pos), where pos is the start of the substring.
  • The first substring isn't null terminated. You can print substrings of any length by providing a "precision" to the %s format after a period: printf("%.4s", str).
  • You can make that precision variable by using an asterisk in the format and then providing an additional int argument before the actual argument: printf("%.*s", 4, str)

This answer is at heart the same answer as fluter's, but it guards against buffer overfloows and involves fewer length calculations.

5 Comments

Actually I'm trying to copy a RingBuffer into a LinearBuffer. Let's assume that the RingBuffer we are dealing with is of type : ' typedef struct { RingBuff_Data_t Buffer[BUFFER_SIZE]; /**< Internal ring buffer data, referenced by the buffer pointers. / RingBuff_Data_t In; /**< Current storage location in the circular buffer / RingBuff_Data_t Out; /**< Current retrieval location in the circular buffer */ RingBuff_Count_t Count; } RingBuff_t;
Aha. That's a different question entirely. (Because you don't have to deal with terminating null characters and because you want to deal with any kind of data.) I guess the positions In and Out are pointers. (The astersks were treated as *italic* markup.)
and my code looks like this : <code> memset(linearBuffer,0,LINEAR_BUFFER_SIZE); RingBuff_Data_t* tempIn = buf.In; if (buf.Out < tempIn){ memcpy(linearBuffer,buf.Out,tempIn-buf.Out); } else if (buf.Out > tempIn){ strcpy(linearBuffer,buf.Out); strncpy(linearBuffer+strlen(buf.Out),buf.Buffer,buf.In-buf.Buffer); } </code> but I'm not sure about it. P.S, how do I add a code so you can read it easily...?
Please, don't post code in comments; it's hard to read. You can post short code snippets in back quotes, but longer code like yours belongs in the question. Edit your question instead. (Or ask a new question.)
In the question, where you can post the code in multiple lines, not as a comment. To edit a code in the question, paste it, select it and then click the button with the two braces, {}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.