代码之家  ›  专栏  ›  技术社区  ›  citronas

如何将源字符串附加在目标字符串之前的两个字符串连接起来?

  •  0
  • citronas  · 技术社区  · 15 年前

    这就是我想到的。不幸的是,我被这些指向字符、字符数组等的指针困住了。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(int argc, char* argv[] )
    {
      char* output;
      int i;
      for(i = 9; i > 0; i--)
      {
        unsigned int value = (unsigned int)i;
        char buffer[20];
        sprintf(buffer, "%u", value);
    //    strcat(ouput, buffer);  // append before the string.
        // first loop: 9
        // second loop: 89
        // third loop: 789
      }
    
      printf("%s", output);
    }
    

    我必须如何更正代码才能使其正常工作?我想我必须以某种方式将输出变量设置为空。什么时候需要字符数组或指针的固定宽度?我只是随便猜了一下。

    5 回复  |  直到 15 年前
        1
  •  3
  •   Community Mohan Dere    12 年前

    我很困惑,因为你发布的代码与你所说的问题完全无关。(好吧,它们都使用字符串,但仅此而已)

      char* src  = "Hello, ";
      char* dest = "World!";
    
      char* temp;
      temp = malloc(strlen(src) +strlen(dest) + 1);
      strcpy(temp, src);
      strcat(temp, dest);
    
      dest = temp;
    

    除非dest是一个固定的缓冲区,其大小足以容纳组合字符串。如果是,则将最后一行替换为:

      strcpy(dest, temp);
      free(temp);
    

    现在,如果您想具体地向后构建数字列表,让我们尝试另一种方法:

      char buffer[10];
      buffer[9] = '\0';  // null terminate our string.
      char* output;    
      int i;    
      for(i = 9; i > 0; i--)    
      {    
           // this is a fast way of saying, sprintf("%u", i); 
           // works only for single digits
           char d = (char)('0' + i); 
    
           buffer[i-1] = d;
           output = &buffer[i-1];
           printf("%s", output);
       }
    
        2
  •  2
  •   Jerry Coffin    15 年前

    通常,你应该从避免这种情况开始。对于您的示例来说,最明显的解决方案是从简单的向上计数开始。当这不合适时,一个递归的解决方案来反转字符串的生成顺序,仍然可以让您从头到尾生成字符串:

    int build_string(int value, char *string) { 
        char temp[10];
    
        if (value > -1)
            build_string(value-1, string);
    
        sprintf(temp, "%d", value); // use snprintf if available.
        strcat(string, temp);
        return string;
    }
    
    int main() { 
        char result[20] = {0};
        build_string(9, result);
        printf("%s", result);
        return 0;
    }
    
        3
  •  1
  •   codaddict    15 年前

    您可以在字符串末尾附加整数,如下所示:

    int i;
    char buffer[20];
    for(i = 0; i < 10; i++) {
            sprintf(buffer+i, "%u", i);
    }
    printf("%s", buffer); // prints 0123456789
    
        4
  •  1
  •   Jonathan Leffler    15 年前

    对于您声明的问题(在另一个字符串前面插入一个字符串),此代码将完成此工作-但没有错误检查。它假定目标缓冲区中有足够的空间容纳现有字符串和新前缀:

    /* Insert string t in front of string s in string s */
    char *strinsert(char *s, const char *t)
    {
        char           *p = s + strlen(s);
        char           *q = p + strlen(t);
        char           *r = s;
    
        while (p >= s)
            *q-- = *p--;
        while (*t)
            *s++ = *t++;
        return(r);
    }
    

        5
  •  1
  •   Steve Jessop    15 年前

    假设目标缓冲区足够大,并且源和目标不重叠:

    // not sure what order to put the params - the usual C way is destination
    // followed by source, but it's also potentially confusing that the result of 
    // prepend(foo,bar) is "<bar><foo>".
    char* prepend(char *restrict dest, const char *restrict src) {
        size_t len = strlen(src);
        memmove(dest + len, dest, strlen(dest));
        return memcpy(dest, src, len);
    }
    

    src dest ),这种方法行不通。

    如果目标缓冲区不够大,那么必须有人为结果分配新的内存,在这种情况下,哪个是“源”哪个是“目的地”的问题就会消失——它们都是“源”,都不是“目的地”。