当前位置:首页 > C++ > 正文内容

C和C++中的字符串

chanra1n4年前 (2020-10-28)C++4185
/*C风格字符串的声明和使用 
#include<cstdio.h>
int main()
{
	char x[]={'H','e','l','l','o',' ','C','+','+','\0'};
	//等效于 char x[]="Hello C++"; 
	int now=0;
	for(now=0;now<strlen(x);now++)
	printf("%c",x[now]); 
	return 0;
}*/
/*C++风格字符串库使用
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string x = "Hello C++";
	cout << x;
	return 0;
}*/

我想到一个问题,如果我连接字符串 s2 到字符串 s1 的末尾,那么字符串的长度会是两个字符串长度的和吗?还是去掉前一个结尾的\0?

#include<iostream>
#include<cstring> 
using namespace std;
int main()
{
	string str1="Hello";
	string str2="Myfpga";
	string str3=str1+str2;
	cout << str1.size() << endl;
	cout << str2.size() << endl;
	cout << str3.size() << endl;
	return 0;
 }

结果是

5
6
11


扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:https://www.myfpga.cn/index.php/post/159.html

分享给朋友:

“C和C++中的字符串” 的相关文章

C++入门 输出Hello World

C++入门 输出Hello World

#include <iostream>using namespace std;int main(){     cout << "Hello, world!...