Tha task of this problem is to count the number of words of one line.
My solution is to insert a space into the beginning of the line.
Here is my code:
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
bool letter(char c)
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return true;
return false;
}
int ans;
string s;
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
while(getline(cin,s))
{
ans=0;
s.insert(0," ");
for(int i=0;i<s.length()-1;i++)
if(!letter(s[i])&&letter(s[i+1]))
ans++;
cout<<ans<<endl;
}
return 0;
}
posted on 2011-01-23 16:51
lee1r 阅读(416)
评论(0) 编辑 收藏 引用 所属分类:
题目分类:基础/模拟