我是靠谱客的博主 威武秋天,这篇文章主要介绍C/C++ 连接两个字符串,现在分享给大家,希望可以做个参考。

一: C风格字符串连接
#include <iostream>
using namespace std;

int main()
{
   const char *str = "hello ";
   const char *str2 = "world";
   const size_t len = strlen(str)+strlen(str2);
   char *n_str = new char[len+1];
   strcpy(n_str,str);
   strcat(n_str,str2);
   cout<<n_str<<endl;
   delete [] n_str;
   return 0;
}

二|:C++ string类型字符串
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const string str="hello ";
   const string str2="world";
   string n_str;
   n_str = str;
   n_str +=str2;
   cout<<n_str<<endl;
   return 0;
}

最后

以上就是威武秋天最近收集整理的关于C/C++ 连接两个字符串的全部内容,更多相关C/C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部