単語の数
Haskellで。
words.hs
main = do cs <- getContents print $ length $ words cs
似たものをRubyで。
words.rb
p ARGV.shift.split(/\s+/).length
あるいは
def words(bun) bun.split(/\s+/).length end p words(ARGV.shift)
似たものをPythonで。
words.py
import sys def word(bun): return bun.split(' ') ans = word(sys.argv[1]) print len(ans)
似たものをc++で。なんだか忘れている。
words.cpp
#include#include #include #include int split(char *wo) { int ans=0; BOOL fl=FALSE; while(*wo!=0){ if(*wo!=' ') { if(!fl) { ans++; } fl=TRUE; } else { fl=FALSE; } wo++; } return ans; } int main(int argc,char **argv) { char word[1000]; if(argc<=1) { return 0; } else { if(strlen(argv[1])>0) strcpy(word,argv[1]); cout << split((char *)word); } return 0; }