核心代码:
cin.get(ch);
while (ch != '.')
{
if (ch == ' ')
++spaces;
++total;
cin.get(ch);
}
整体代码:
#include<iostream>
using namespace std;
int main()
{
char ch;
int spaces = 0;
int total = 0;
cin.get(ch);
while (ch != '.')
{
if (ch == ' ')
++spaces;
++total;
cin.get(ch);
}
cout << spaces << "空白" << endl;
cout << total << "个字符在本句中(包含回车符的换行符)";
system("pause");
return 0;
}
运行结果: