求一个字符串中空格字符的个数。
一行字符,前、后及中间有数量不定的空格。
例子输入: three !
例子输出:4
[解决办法]
int count_non_empty(iostream &os = cin)
{
int count = 0;
char c;
while(c = os.getchar)
{
if(c == ' ')
{
++count;
}
return count;
}
[解决办法]
#include<iostream>using namespace std;int count_non_empty(char* p){ int count = 0; while(*p != '\0') { if(*p == ' ') { ++count; } p++; } return count;}int main(){ char a[32]={0};int b; gets(a); b=count_non_empty(a); cout<<b; return 0;}
[解决办法]