From: Joao Silva Date: 2012-07-07T02:50:58+09:00 Subject: Re: Management of words in a string "Jesús Gabriel y Galán" wrote in post #1067632: > You can use String#split method. You have to define very well what is > a word for you. For example, consider things like "one-way street" or > "it's raining", and also be careful with punctuation. A simplistic > approach could be just to use the default split behaviour, which > splits by the spaces: > > s = "this has words. how many? let's see" > [5] pry(main)> s.split > => ["this", "has", "words.", "how", "many?", "let's", "see"] > [6] pry(main)> s.split.size > => 7 > > You can pass a regular expression to the split method to tune how you > split. > > Jesus. and in case you want to count the words that begin with a particular letter (for example "a"). ############################################## ct=0 print "Enter a string: " str=gets.chomp.to_s puts "Word ==> #{str.split}" if str.chr == "a" ct=ct+1 end puts "Number of words that start with a: #{ct}" ################################################# -- Posted via http://www.ruby-forum.com/.