Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中初始化未知值字符串的正确方法_C_String - Fatal编程技术网

C中初始化未知值字符串的正确方法

C中初始化未知值字符串的正确方法,c,string,C,String,假设我想创建一个字符串,该字符串将基于另一个字符串保存一些值。基本上,我希望能够压缩一个字符串,如下所示:aaabb->a3b2-但我的问题是: 在Java中,您可以执行以下操作: String mystr = ""; String original = "aaabb"; char last = original.charAt(0); for (int i = 1; i < original.length(); i++) { // Some code not relevant

假设我想创建一个字符串,该字符串将基于另一个字符串保存一些值。基本上,我希望能够压缩一个字符串,如下所示:aaabb->a3b2-但我的问题是:

在Java中,您可以执行以下操作:

String mystr = "";
String original = "aaabb";

char last =  original.charAt(0);
for (int i = 1; i < original.length(); i++) {
  // Some code not relevant
  mystr += last + "" + count; // Here is my doubt.
}
char mystr[1024];
char * str = "abcdef";
char c = str[1];  // will get 'b'
int int_num = 100;
sprintf(mystr, "%s%c%d", str, c, int_num);
字符串mystr=”“;
字符串original=“aaabb”;
char last=original.charAt(0);
对于(int i=1;i

如您所见,我们已经初始化了一个空字符串,可以对其进行修改(
mystr+=last+“”+count;
)。在C中如何做到这一点?

不幸的是,在C中,您不能像在Java中那样轻松:字符串内存需要动态分配

这里有三种常见的选择:

  • 尽可能多地分配所需的资源,然后在完成后修剪到合适的大小-这是很常见的,但也有风险,因为在错误计算最大值时,可能会出现缓冲区溢出
  • 运行算法两次-第一次计算长度,第二次填充数据-如果时间由内存分配决定,这可能是最有效的一次:这种方法只需要分配一次,并且分配精确的内存量
  • 随用分配-从一个短字符串开始,然后在需要更多内存时使用
    realloc

  • 我建议使用第二种方法。在您的示例中,您将遍历源字符串一次以计算压缩长度(在您的示例中,有效负载为5-4个字符
    “a3b2”
    ,一个用于空终止符。有了这些信息,您可以分配五个字节,然后使用分配的缓冲区进行输出,这保证是合适的。

    不幸的是,在C语言中,您不能像在Java中那样轻松:字符串内存需要动态分配

    这里有三种常见的选择:

  • 尽可能多地分配所需的资源,然后在完成后修剪到合适的大小-这是很常见的,但也有风险,因为在错误计算最大值时,可能会出现缓冲区溢出
  • 运行算法两次-第一次计算长度,第二次填充数据-如果时间由内存分配决定,这可能是最有效的一次:这种方法只需要分配一次,并且分配精确的内存量
  • 随用分配-从一个短字符串开始,然后在需要更多内存时使用
    realloc
  • 我建议使用第二种方法。在您的情况下,您将运行源字符串一次以计算压缩长度(在您的情况下,有效负载为5-4个字符
    “a3b2”
    ,一个用于空终止符。有了这些信息,您可以分配五个字节,然后使用分配的缓冲区进行输出,这是保证适合的。

    在C(不是C++)中,您可以执行以下操作:

    String mystr = "";
    String original = "aaabb";
    
    char last =  original.charAt(0);
    for (int i = 1; i < original.length(); i++) {
      // Some code not relevant
      mystr += last + "" + count; // Here is my doubt.
    }
    
    char mystr[1024];
    char * str = "abcdef";
    char c = str[1];  // will get 'b'
    int int_num = 100;
    sprintf(mystr, "%s%c%d", str, c, int_num);
    
    这将在“mystr”中创建一个字符串:

    "abcdefb100"
    
    然后可以使用strcat()将更多数据连接到此字符串

    请注意,mystr已声明为1024字节长,这是您可以在其中使用的所有空间。如果您知道字符串的长度,可以使用C中的malloc()来分配空间,然后使用它

    如果您想使用C++,它有更强大的处理字符串的方法。

    在C(不是C++)中,您可以执行以下操作:

    String mystr = "";
    String original = "aaabb";
    
    char last =  original.charAt(0);
    for (int i = 1; i < original.length(); i++) {
      // Some code not relevant
      mystr += last + "" + count; // Here is my doubt.
    }
    
    char mystr[1024];
    char * str = "abcdef";
    char c = str[1];  // will get 'b'
    int int_num = 100;
    sprintf(mystr, "%s%c%d", str, c, int_num);
    
    这将在“mystr”中创建一个字符串:

    "abcdefb100"
    
    然后可以使用strcat()将更多数据连接到此字符串

    请注意,mystr已声明为1024字节长,这是您可以在其中使用的所有空间。如果您知道字符串的长度,可以使用C中的malloc()来分配空间,然后使用它


    如果您想使用C++,它有更健壮的处理字符串的方法。

    您可以使用字符串串联方法
    strcat

    您可以按以下方式定义字符串:

    char mystr[1024]; // Assuming the maximum string you will need is 1024 including the terminating zero
    
    char countString[32];
    itoa (count, countString, 10); // Convert count to decimal ascii string
    
    strcat(mystr, lastString);
    strcat(mystr, countString);
    
    要将字符
    last
    转换为字符串,以便能够将其连接起来,请使用以下语法:

    char lastString[2];
    lastString[0] = last; // Set the current character from the for loop
    lastString[1] = '\0'; // Set the null terminator
    
    要将计数转换为字符串,需要使用
    itoa
    函数,如下所示:

    char mystr[1024]; // Assuming the maximum string you will need is 1024 including the terminating zero
    
    char countString[32];
    itoa (count, countString, 10); // Convert count to decimal ascii string
    
    strcat(mystr, lastString);
    strcat(mystr, countString);
    
    然后您可以按如下方式使用strcat:

    char mystr[1024]; // Assuming the maximum string you will need is 1024 including the terminating zero
    
    char countString[32];
    itoa (count, countString, 10); // Convert count to decimal ascii string
    
    strcat(mystr, lastString);
    strcat(mystr, countString);
    

    另一个解决方案是使用STL字符串类或MFC cString如果使用Visual C++ +

    可以使用字符串级联方法<代码> STRCAT/CODE>

    您可以按以下方式定义字符串:

    char mystr[1024]; // Assuming the maximum string you will need is 1024 including the terminating zero
    
    char countString[32];
    itoa (count, countString, 10); // Convert count to decimal ascii string
    
    strcat(mystr, lastString);
    strcat(mystr, countString);
    
    要将字符
    last
    转换为字符串,以便能够将其连接起来,请使用以下语法:

    char lastString[2];
    lastString[0] = last; // Set the current character from the for loop
    lastString[1] = '\0'; // Set the null terminator
    
    要将计数转换为字符串,需要使用
    itoa
    函数,如下所示:

    char mystr[1024]; // Assuming the maximum string you will need is 1024 including the terminating zero
    
    char countString[32];
    itoa (count, countString, 10); // Convert count to decimal ascii string
    
    strcat(mystr, lastString);
    strcat(mystr, countString);
    
    然后您可以按如下方式使用strcat:

    char mystr[1024]; // Assuming the maximum string you will need is 1024 including the terminating zero
    
    char countString[32];
    itoa (count, countString, 10); // Convert count to decimal ascii string
    
    strcat(mystr, lastString);
    strcat(mystr, countString);
    

    另一个解决方案是使用VisualC++的STL字符串类或MFC cString。< /P>您不修改字符串。java字符串是不可变的。您正在创建一个新的字符串并指向MySTR到新字符串。C字符串只是字符数组(可以修改)。它可以用SaveTf和StrCAT来完成,但是要准备好:对于字符串操作,C是一个瓶颈。不能使用C++ +?@ StRetrk18。您正在创建一个新的<代码>字符串< /> >并将引用存储到<代码> MySTR< /COD>中。这就是为什么代码> StringBuilder < /C>有时可以提供性能增益。(尽管在许多情况下,编译器会在后台放置一个

    StringBuilder
    来添加字符串附件)。@StarTrek18使用C很容易,这取决于您对“easy”的定义。最简单的方法可能是设置一个固定大小的缓冲区并使用前面提到的
    strcat
    。您也可以根据需要分配一个缓冲区并使用,例如,
    realloc()
    ,使其增长。@v3ga我的意思是
    StringBuffer
    ,抱歉,但是是的。请参阅,并且您没有修改字符串。Java字符串是