题目:
将一个短语转换为它的首字母缩略词。如下给出了程序的 输入 和 输出 示例。
| 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 ""]]
}