#UVa:494-Kindergarten Counting Game

灆洢 2011-11-29 00:13:46

尋找字母與非字母之間的交界處有幾個就可以知道有幾個字了。

P.S. 不能單純只用空格來分隔單字,測資當中會有類似 This!has,four.words? 是4個字的這種測資。

C++(0.020)

/*******************************************************/
/* UVa 494 Kindergarten Counting Game                  */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;

int main(){
  string s, temp;
  int count;
  bool alphap = 0;
  while( getline( cin, s ) ){
    count = 0;
    for( int i = 0 ; i < s.length() ; i++ ){
      if( isalpha(s[i]) && !alphap ){
        alphap = 1;
        count++;
      }
      else if( !isalpha(s[i]) && alphap ){
        alphap = 0;
      }
    }
    printf( "%d\n", count );
  }
  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料