Mike Sanders
2023-10-01 09:38:01 UTC
run as...
awk -f uniqueChars.awk
output...
Input string: Mary had a little lamb who's fleece was white as snow...
Unique chars: Mary hdlitembwo'sfcn.
script...
BEGIN {
a = "Mary had a little lamb who's fleece was white as snow..."
b = uniqueChars(a)
print "Input string: " a
print "Unique chars: " b
}
function uniqueChars(str, x, y, c, tmp, uniqueStr) {
y = length(str)
uniqueStr = ""
delete tmp # clear array for each new string
while(++x <= y) {
c = substr(str, x, 1)
if (!(c in tmp)) {
uniqueStr = uniqueStr c
tmp[c]
}
}
return uniqueStr
}
awk -f uniqueChars.awk
output...
Input string: Mary had a little lamb who's fleece was white as snow...
Unique chars: Mary hdlitembwo'sfcn.
script...
BEGIN {
a = "Mary had a little lamb who's fleece was white as snow..."
b = uniqueChars(a)
print "Input string: " a
print "Unique chars: " b
}
function uniqueChars(str, x, y, c, tmp, uniqueStr) {
y = length(str)
uniqueStr = ""
delete tmp # clear array for each new string
while(++x <= y) {
c = substr(str, x, 1)
if (!(c in tmp)) {
uniqueStr = uniqueStr c
tmp[c]
}
}
return uniqueStr
}
--
:wq
Mike Sanders
:wq
Mike Sanders