Reputation: 1
how to find the count of uppercase amd lower case in tcl with this code im getting only ascii values
foreach character {H e l l o T C L} {
scan $character %c numeric
puts "ASCII character '$numeric' displays as '$character'."
}
Upvotes: 0
Views: 263
Reputation: 4813
Instead of looping through a string yourself, you can use regexp
to give you a count:
set str "Hello Tcl"
puts "Uppercase: [regexp -all {[[:upper:]]} $str]"
puts "Lowercase: [regexp -all {[[:lower:]]} $str]"
I'm using [[:upper:]]
and [[:lower:]]
instead of [A-Z]
and [a-z]
because the former will correctly capture unicode upper- and lowercase, rather than just the ones in the ASCII set.
Upvotes: 3
Reputation: 4382
You can test each character with string is upper $character
and string is lower $character
. Note that non-alphabetic characters are neither upper or lower case. For more info check the documentation at https://www.tcl-lang.org/man/tcl8.6/TclCmd/string.htm#M10
Upvotes: 2