C++ 获取字符串的长度
1.字符串的长度获取
有两种方法,一种是length(),一种是size()
string str1="123";
cout<<str1.length()<<endl;//输出3
cout<<str1.size()<<endl;//输出3
2.字符数组的长度获取
strlen方法获取实际长度,sizeof获取申明的数组所占字节数
char a[100]={'1','2','3'};
char b[100]={"123"};
char c[100];
cin>>c;//输入123
cout<<strlen(a)<<endl;//输出3
cout<<strlen(b)<<endl;//输出3
cout<<strlen(c)<<endl;//输出3
cout<<sizeof(a)<<endl;//输出100
cout<<sizeof(b)<<endl;//输出100
cout<<sizeof(c)<<endl;//输出100
带空格的字符串输入:
https://blog.csdn.net/qq_40659901/article/details/104855002