【问题标题】:How to concatenate strings in an output function?如何在输出函数中连接字符串?
【发布时间】:2020-09-13 23:16:16
【问题描述】:

有些语言有简单的方法来做到这一点,但我的问题是 C 和 C++。

我想在 Java 中做这样的事情:

public class sandbox {
    public static void main(String[] args) {
        System.out.println("Thank" + " you!");
    }
}

并将其转移到 C 中:

#include <stdio.h>

int main() {
    /* The easiest way is like this:
        char *text1 = "Thank";
        char *text2 = " you";
        printf("%s%s\n", text1, text2);
    */
    printf("Thank" + " you."); // What I really want to do
}

如何在这样的语言中连接字符串?

【问题讨论】:

  • C 文件的注释部分有什么问题?
  • C++ 和 C 是不同的东西。
  • 当您也将此问题标记为 C++ 时,为什么不使用 std::string?支持字符串连接。
  • 无论是 C 还是 C++,答案都是不同的
  • C 的答案是strcat(或strncat 函数),但它需要了解字符串是什么以及它是如何存储在内存中的。基本上,字符串是一个以 nul 结尾的字符数组,并且连接的目标缓冲区必须有足够的空间容纳新字符串。 C 中没有自动重新分配。

标签: c++ c


【解决方案1】:

你什么都不用:

puts ("Thank" " you.");

【讨论】:

  • 如果字符串通常是文字,我很确定这个问题不会被问到......
  • 是的——这是退化的情况。
【解决方案2】:

不幸的是,在 C 中连接字符串并不容易,下面是最简洁的方法:

char *text1 = "Thank";
char *text2 = " you";

char *text_concat = malloc(strlen(text1) + strlen(text2) + 1);
assert(text_concat);

text_concat = strcpy(text_concat, text1);
text_concat = strcat(text_concat, text2);

printf("%s\n", text_concat);

free(text_concat);

【讨论】:

    【解决方案3】:

    我从你的问题中了解到,希望下面的解决方案能回答你的问题。

    #include <stdio.h>
    
    int main() {
      char s1[100] = "Thank ", s2[] = "You";
      int length, j;
    
      // store length of s1 in the length variable
      length = 0;
      while (s1[length] != '\0') {
        ++length;
      }
    
      // concatenate s2 to s1
      for (j = 0; s2[j] != '\0'; ++j, ++length) {
        s1[length] = s2[j];
      }
    
      // terminating the s1 string
      s1[length] = '\0';
    
      printf("After concatenation: %s",s1);
    
    
      return 0;
    }
    

    在 C++ 中,您可以通过使用 + 运算符添加两个 string 轻松地连接两个字符串。

    #include <iostream>
    using namespace std;
    
    int main()
    {
        string s1, s2, result;
    
        cout << "Enter string s1: ";
        cin>>s1;
    
        cout << "Enter string s2: ";
        cin>>s2;
    
        result = s1 + s2;
    
        cout << "After concatenation: = "<< result;
    
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      这是一个连接,但是是一个常量或编译时连接,你不能像这样连接字符串,但是如果你需要将一个字符串常量拆分成多个部分是可以的:

      ...
          printf("Thank"  " you."); // What I really want to do
      ...
      

      对于动态的运行时连接,您需要类似 strcat

      strcat(text1, text2);
      

      首先您必须确保目标字符串中有足够的内存,请参阅此链接http://www.cplusplus.com/reference/cstring/strcat/
      好的,那是 C 的方式,但 C++ 有带 std::string 的 STL

      #include <iostream>
      #include <string>
      using namespace std;
      int main()
      {
          string str1 = "hello ", str2 = "world";
          cout<< str1 + str2<< endl;
          return 0;
      }
      
      

      【讨论】:

      • 如果你只是打印,那么你不需要 C++ 中的 +,只需使用
      • @kelalaka 它是关于串联
      • 构造一个新的字符串对象然后打印输出是一个代价高昂的操作。
      • @kelalaka 是关于如何使用连接的。
      【解决方案5】:

      在 C 中不可能做类似printf("Thank" + " you."); 的事情,因为 C 不像 C++ 那样支持运算符重载。可以参考Is it possible to overload operators in C?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-12
        • 1970-01-01
        • 2021-02-15
        相关资源
        最近更新 更多