UVa 494 Kindergarten Counting Game

UVa 494 Kindergarten Counting Game
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 ;
}

你可能感兴趣的:(UVa 494 Kindergarten Counting Game)