想要真正的学会一门编程语言,最重要的还是要动手开始写,有些时候是一看就懂,到自己写了,就不知道怎么办了,所以接下来会给到大家编程练习题,以及正确的 tcl 编写的答案,当然你也可以通过其它如 python 等语言来练习。
不管你现在在学什么编程语言,都可以跟着练习,欢迎大家分享自己编写的程序~~~
题目:
将一个短语转换为它的首字母缩略词。如下给出了程序的 输入 和 输出 示例。
Input | Output |
As soon as possible | ASAP |
I'm fine, and you? | IFAY |
hello-world, hello-tcl. | HWHT |
_init_design this is also be ok! | IDTIABO |
参考答案一:
proc abbreviate {phrase} {
set firstLetters [regexp -all -inline {[[:alpha:]][[:alpha:]']*} $phrase]
set acronym ""
foreach letter $firstLetters {
append acronym [string index $letter 0]
}
return [string toupper $acronym]
}
参考答案二:
proc abbreviate {phrase} {
set tokens [split $phrase " -_"]
foreach token $tokens {
lappend res [regexp -nocase -inline {[a-z]} $token]
}
return [string toupper [join $res ""]]
}