Advertisement
zvoulgaris

Untitled

Oct 8th, 2019
2,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.94 KB | None | 0 0
  1. # Scramble words in a string
  2.  
  3. using Random
  4.  
  5. function ScrambleWord(word::AbstractString)
  6.     n = length(word)
  7.     letters = split(word, "")
  8.     upper = falses(n)
  9.  
  10.     for i = 1:n
  11.         if letters[i] == uppercase(letters[i]); upper[i] = true; end
  12.     end
  13.  
  14.     letters = letters[randperm(n)]
  15.  
  16.     for i = 1:n
  17.         if upper[i]
  18.             letters[i] = uppercase(letters[i])
  19.         else
  20.             letters[i] = lowercase(letters[i])
  21.         end
  22.     end
  23.  
  24.     return join(letters, "")
  25. end
  26.  
  27. function main(s::AbstractString)
  28.     dividers = [' ', ',', '.', ';', ':', '!', '`', '\"', '\'', '-', '(', ')']
  29.     N = length(s)
  30.     Z = Array{AbstractString}(undef, N)
  31.     c = 0
  32.     ind = 1
  33.  
  34.     for i = 1:N
  35.         if s[i] in dividers
  36.             c += 1
  37.             Z[c] = ScrambleWord(s[ind:(i-1)])
  38.             c += 1
  39.             Z[c] = string(s[i])
  40.             ind = i + 1
  41.         end
  42.     end
  43.  
  44.     return join(Z[1:c], "")
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement