-1

What is the best way to take a word and split it up between 2 to 4 character in length?

Examples:

  • "tundra" could be "tun" & "dra" or "tund" & "ra"
  • "rainforest" could be "ra" & "in" "fore" & "st"

Is there a method designed specifically to accomplish this?

Tim Post
  • 32,782
  • 15
  • 106
  • 168
Chad
  • 273
  • 2
  • 8
  • 18
  • 3
    Whats the pattern you are looking for exactly? Why cant "rainforest" be "ra", "in", "fo", "re" and "st"? – Shiva Kumar Feb 10 '13 at 06:59
  • It can be. I am given a word with a length range of 4 to 16 characters, I need to break that word down to subworsd in a range of 2 to 4 characters that make up the original word. – Chad Feb 10 '13 at 07:03
  • You mean you need to come up will all combinations of 2-4 sized subwords of the original word? – Shiva Kumar Feb 10 '13 at 07:07
  • Yes, Given a list of 7 words that vary in length from 4 to 16 characters I need to return a list of 20 strings making up those words. thanks. – Chad Feb 10 '13 at 07:09

2 Answers2

0

Why not use modulo operation on string length? With the result you can easily split your string in some pieces.

Alina B.
  • 1,246
  • 8
  • 17
0

Look at this question on how to split at string at every nth position. You then just need to iterate over n from 2 to 4 to get all your substrings.

Community
  • 1
  • 1
Shiva Kumar
  • 3,071
  • 1
  • 25
  • 33