vb.net - .NET Counting vowels from a text file -
as newbie .net decided set myself little challenge of writing program count number of vowels input string. program below works fine, then, develop further thought, instead of entering string via console.readline, possible extract text text file? tried using fileopen/fileclose , streamreader approach resolve issue mind set text file should converted chararray. looking @ problem wrong angle? can customise existing code , adapt text files, or need adopt new approach?
any appreciated, thanks
module module1 sub main() dim counter1 integer = 0 console.writeline("enter word") dim myword string = console.readline() dim chararray() char = myword.tochararray() each letter in chararray select case letter case "a", "e", "i", "o", "u", "a", "e", "i", "o", "u" counter1 = counter1 + 1 end select next console.writeline("the number of vowels in word " & counter1) console.readline() end sub end module
you can in 1 line:
console.writeline(regex.matches(file.readalltext("c:\input.txt"), "[aeiouaeiou]").count)
file
in system.io
namespace, , regex
in system.text.regularexpressions
namespace.
just note this: while it's pretty rad can in 1 line, it's not best things this. keep method calls out in own statements because makes easier debug when can see value returned each one.
Comments
Post a Comment