简体   繁体   English

从2个子字符串创建一个字符串

[英]creating a string from 2 substrings

I want to create a big string 'des' from 2 substrings copied from string 'scr' this way : 我想通过这种方式从字符串'scr'复制的2个子字符串中创建一个大字符串'des':

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. 我想复制该子字符串,让我们称它为“ string1”(从ptr x到“ scr”字符串的末尾),然后将其放在“ des”中,然后将其与另一个子字符串连接,让我们调用它-将'scr'字符串中的'string 2'从字符串的开头复制到x ptr。 How can I do it not using a temp string ? 我怎么不使用临时字符串呢?

for example : scr = "ThisIs", string1 = "Is", string2 = "This" des = "IsThis" 例如:scr =“ ThisIs”,string1 =“ Is”,string2 =“ This” des =“ IsThis”

I don't want to use a temp string to hold string 1 or string2. 我不想使用临时字符串来保存字符串1或string2。

Can you help me? 你能帮助我吗?

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);

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. snprintf将格式化的数据写入字符串,就像printf将格式化的数据写入屏幕一样。 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. 它还保证结果字符串以null结尾。
  • 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. snprintf返回如果缓冲区很小时字符串将具有的长度。 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. 第二个子字符串以空值结尾,因为它在整个字符串src结束处结束。 You can print it with printf("%s", str + pos) , where pos is the start of the substring. 您可以使用printf("%s", str + pos)进行打印,其中pos是子字符串的开头。
  • The first substring isn't null terminated. 第一个子字符串不是以null结尾的。 You can print substrings of any length by providing a "precision" to the %s format after a period: printf("%.4s", str) . 通过在句点后为%s格式提供“精度”,可以打印任意长度的子字符串: 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) 您可以通过在格式中使用星号,然后在实际参数之前提供一个附加的int参数来使该精度变量: 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. 这个答案与flutter的答案在本质上是相同的,但是它可以防止缓冲区溢出,并减少了长度计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM