Determine how many symmetric words are in a given string.


  • 1
    $\begingroup$ Welcome to Mathematica Stack Exchange. What exactly is your question? Please take a moment to take the tour and familiarize yourself with the kind of questions that attract high-quality answers. $\endgroup$ – Roman Jun 3 at 19:16
  • $\begingroup$ What have you tried? What code have you written? What problems resulted from running it? $\endgroup$ – David G. Stork Jun 3 at 19:36
  • $\begingroup$ What is a symmetric word, is it a palindrome? $\endgroup$ – C. E. Jun 3 at 19:51
  • $\begingroup$ symmetrical words that are the same read from left to right and from right to left for example "bobhousesam" answer bob $\endgroup$ – Ivan912 Jun 3 at 21:16
StringCount["bobhousesam", x:(_~~__)/;PalindromeQ[x], Overlaps -> All]


StringCases["bobhousesam", x:(_~~__)/;PalindromeQ[x], Overlaps -> All]

{"bob", "ses"}


_~~__ is a string pattern which stands for two-or-more characters (strictly, one character followed by a one-or-more characters. See Blank, BlankSequence and StringExpression in the docs.

We can use it to extract substrings with two or more characters:

StringCases["bobhousesam", _ ~~ __, Overlaps -> All]

{"bobhousesam", "bobhousesa", "bobhouses", "bobhouse", "bobhous", "bobhou", "bobho", "bobh", "bob", "bo", "obhousesam", "obhousesa", "obhouses", "obhouse", "obhous", "obhou", "obho", "obh", "ob", "bhousesam", "bhousesa", "bhouses", "bhouse", "bhous", "bhou", "bho", "bh", "housesam", "housesa", "houses", "house", "hous", "hou", "ho", "ousesam", "ousesa", "ouses", "ouse", "ous", "ou", "usesam", "usesa", "uses", "use", "us", "sesam", "sesa", "ses", "se", "esam", "esa", "es", "sam", "sa", "am"}

(Try it without the Overlaps -> All option to see why we need to add this option.)

x:(_ ~~ __) is a pattern named x (see Pattern) that stands for two-or-more characters. StringCases["bobhousesam", x:(_ ~~ __), Overlaps -> All] yields the same output as above.

We use the names to attach conditions (see Condition) to patterns. The patern with condition x:(_~~__)/;PalindromeQ[x] is a string expression named x with two or more characters such that PalindromeQ[x] yields True.

StringCases["bobhousesam", x : (_ ~~ __) /; PalindromeQ[x], Overlaps -> All]

{"bob", "ses"}

Note that if we had used x:(__)/;PalindromeQ[x] we would have had

StringCases["bobhousesam", x : (__) /; PalindromeQ[x], Overlaps -> All]

{"bob", "b", "o", "b", "h", "o", "u", "ses", "s", "e", "s", "a", "m"}

  • $\begingroup$ can you please explain this? x:(_~~__)/;PalindromeQ[x] $\endgroup$ – Ivan912 Jun 4 at 3:32
  • $\begingroup$ just not quite clear $\endgroup$ – Ivan912 Jun 4 at 3:50
  • $\begingroup$ @Ivan912, _~~__ is a ` string expression pattern which stands for one character followed by a one-or-more characters (see Blank, BlankSequence and StringExpression; x:pattern is a pattern named x (see Pattern); ... $\endgroup$ – kglr Jun 4 at 3:52
  • $\begingroup$ ... something/; condition[something] is a pattern which matches only if the evaluation of condition[something] yields True (see Condition). Altogether, x:(_~~__)/;PalindromeQ[x] is "a string expression x with two or more characters such that PalindromeQ[x] yields True". $\endgroup$ – kglr Jun 4 at 3:52
  • $\begingroup$ Thank you, can you somehow print out every step of it to see how it all works? $\endgroup$ – Ivan912 Jun 4 at 4:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.