- 字符数组和字符串用法差不多,如果要操作下标就定义字符数组。
- 字符数组求长度用strlen()函数,字符串类型求长度用.length()长度。
- 字符数组的终结字符为‘/0’。
#include <iostream> #include <string.h> using namespace std; int main(){ char a[100]="hello world!"; //char a[100]={0}; cout<<a<<endl; cout<<strlen(a)<<endl; //cout<<a.length()<<endl; string str="hello world!"; cout<<str.length()<<endl; //cout<<"str: "<<strlen(str)<<endl; }