\0空字符
字符数组根据\0来判断字符串的结束
\0空字符
字符数组根据\0来判断字符串的结束
char website[]{'b','a','i','d','u','\0'};
char website[]="baidu";
cout<<strlen(website) 查看字符串chang'd
'\0' 表示空字符
C语言中通过'\0'识别字符串的结束
例如
char a[] = { 'a' , 'b' , 'c' , '\0' }
则输出数组为 abc
若赋值数组为
char a[] = { 'a' , 'b' , '\0' , 'c' }
则输出数组为 ab
也可直接赋值为
char a[] = "abc"
字符串有3个字符,而数组a的长度为4
'\0'自动补全
也可通过
char a[4];
cin >> a;
来使用户输入字符串
可用双引号来对字符串进行分割,方便阅读
例如
char a[] = "My name is Li. I like china."
可写为
char a[] = "My name is Li."
"I like china."
char website[] = { 's', 'i','k','i','e','d','u','\0' }; 加入/0就是字符串 /0代表字符串的结束
字符串会自动拼接
#include <iostream>
using namespace std;
int main()
{
char website[]={'s','i','k','i','e','d','u'};
char website2[]={'s','i','k','i','e','d','u','\0'}; // 空字符 空格字符
cout<<website2<<endl; // 输出:sikiedu
cout<<website<<endl; // 输出:sikiedu烫烫??P|?
char website[] = "sikiedu";
cout<<website<<endl; // 输出:sikiedu
cout<<website[7]<<endl; // 输出:“空白字符”
cout<<strlen(website)<<endl; // 输出:7
cout<<website[3]<<endl; // 输出:i
char website[20];
cin>>website; // sikiedu.com sikiedu.com\0
cout<<website<<endl;
char str[] = "My name is Micheal. I come from china. I like food! I like tralving! "
char str[] = "My name is Micheal. I come from china."
"I like food! I like tralving! "; // 拼接字符串,书写便捷
return 0;
}
char website[]={'s','i','k','i','\0','d','u'}; // 输出:siki
定义字符串数组时,要加\0空字符结束
\0表示字符串结束
char webset[]="jbgysafuay"