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

C和C++中的字符串

chanra1n4年前 (2020-10-28)C++4147
/*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++_2

一小时搞定C++_2

变量是什么?变量就是可以储存值的量,分为局部变量和全局变量,等用到了再说!变量有什么类型?1、int类型 整数类型,只能用来保存整数2、long类型 长整数类型,只能用来保存整数,但是保存的数的范围更多3、float类型 浮点数类型,绝大多数,带小数的4、double类型 双精度浮点数类型,绝大多数...

一小时搞定C++_0 前言 必看

一小时搞定C++_0 前言 必看

本教程内所有代码均不包含运行的结果,请大家先看https://www.myfpga.cn/?id=9https://www.myfpga.cn/?id=11...

C++入门 输出Hello World

C++入门 输出Hello World

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

数据类型及其占用空间

数据类型及其占用空间

#include<iostream> using namespace std; int main() { cout << "The size of int is&nb...

Break和Continue的区别

Break和Continue的区别

#include<iostream>  using namespace std; int main() { int x=0; for(x=0;x<10;x++) { if(x==3) break;...