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

C和C++中的字符串

chanra1n6年前 (2020-10-28)C++7135
/*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://myfpga.cn/index.php/post/159.html

分享给朋友:

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

C++ 中的变量类型创建和使用

C++ 中的变量类型创建和使用

      C++中需要掌握的只有int(整型)float (浮点)char (字符),其他的可自行百度后学习,但是基本上用不到      int float char 这三种类型,换成说人话的解释就是:   ...

c++类的构造函数

c++类的构造函数

 c++的目标之一是让使用类对象就像使用标准类型一样, 也就是说,常规的初始化语法不适用于类型Stock: int year=2001;  struct thing    {      char *pn;      int m;      };      thing amabob={"w...

一小时搞定C++_4

一小时搞定C++_4

 #include <iostream>  using namespace std; int addnum(int a,int b) { return a+b; } int ...

一小时搞定C++_5

一小时搞定C++_5

讲到函数就可以讲讲局部变量和全局变量了,什么是局部变量呢?什么又是全局变量? #include <iostream>  using namespace std; int x,y,z;//x,y,z是全局变量  i...

C++的继承和派生

C++的继承和派生

#include <iostream> using namespace std; class fenshu { public: int xuehao; int yuwen;  }; ...

C++入门 输出Hello World

C++入门 输出Hello World

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