C语言类型字符串
- string类类型
c语言风格字符串
该字符串结果为\0.往往字符数组的大小与实际单词存储的字符多1、
// 第一种初始化方式
char name[7] = {'c','h','i','n','a','I'}
// 第二种初始化方式
char name[] = "china112"
#include <iostream>
using namespace std;
int main()
{
char name[] = "china";
for (int i = 0; i <=5 ; ++i) {
cout<<name[i]<<endl;
}
cout<<"------------"<<endl;
return 0;
}
-
- 可以看到,打印china所有字符串的时候,最后一位为空行,也就是\0。
#include <iostream>
using namespace std;
int main()
{
char name[] = "china";
for (int i = 0; i <=5 ; ++i) {
cout<<name[i]<<endl;
}
cout<<"------------"<<endl;
for (int i = 0; i <=5 ; ++i) {
cout<<*(name+i)<<endl;
}
cout<<"------------"<<endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[13] = "chenweifeng";
char str2[13] = " is cool";
char str3[13];
int len ;
// 复制 str1 到 str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// 连接 str1 和 str2
strcat(str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// 连接后,str1 的总长度
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0;
}
- string
#include <iostream>
#include <string>
using namespace std;
int main(){
string name = "chenweifeng";
string description = " is cool ";
int length,length_2;
string tempString;
tempString = name;
cout<<tempString<<endl;
tempString = name+description;
cout<<tempString<<endl;
length = tempString.size();
length_2 = tempString.length();
cout<<length<<"test"<<length_2<<endl;
return 0;
}