# How many symmetric words are in a string? [closed]

Determine how many symmetric words are in a given string.

https://reference.wolfram.com/language/tutorial/WorkingWithStringPatterns.html

• 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. – Roman Jun 3 at 19:16
• What have you tried? What code have you written? What problems resulted from running it? – David G. Stork Jun 3 at 19:36
• What is a symmetric word, is it a palindrome? – C. E. Jun 3 at 19:51
• symmetrical words that are the same read from left to right and from right to left for example "bobhousesam" answer bob – Ivan912 Jun 3 at 21:16

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

2

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

{"bob", "ses"}

Steps:

_~~__ 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"}