28 lines
727 B
C++
28 lines
727 B
C++
|
// 从键盘输入字符
|
||
|
#include <iostream>
|
||
|
#include <cstdlib>
|
||
|
#include <cstring>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
char name[50] = ""; // 字符数组的初始值建议给一个空字符串, 即 ""
|
||
|
cout
|
||
|
<< "请输入姓名: ";
|
||
|
cin >> name;
|
||
|
|
||
|
// sizeof(字符数组)计算数组分配内存空间的大小
|
||
|
cout << "你输入的内容为" << name << ", 它的字节长度为" << sizeof(name) << endl;
|
||
|
// strlen(字符数组)输出第一个`\0`之前的字符个数
|
||
|
cout << "字符个数为" << strlen(name) << endl;
|
||
|
|
||
|
int i = 0;
|
||
|
while (name[i])
|
||
|
{
|
||
|
cout << name[i++] << "\t";
|
||
|
}
|
||
|
cout << (int)name[i] << endl; // 0 ,用于验证`\0`的 ASCII 码为 0
|
||
|
|
||
|
return 0;
|
||
|
}
|