0
How to "extract palindrome words from sentence"?
6 Réponses
+ 5
Like Nikhil said, separate words based on an appropriate set of delimiters (e.g., whitespace characters).
You could, for example, use one index to find the beginning of a word, and then another index in an embedded loop to continue to the end of the word. You'd then have two word boundary indices.
From there, just compare the first character and the last, the second and the second last, and so on, until the two boundaries intersect. If they intersect and you still haven't rejected the word, it's a palindrome.
I wrote up some code for this if you want to play with it. Any non-alphanumeric character is treated as a delimiter, so you may need to modify it if that matters to you: https://code.sololearn.com/cwnNUtyHYpOA/#cpp
+ 4
take your palindrome in a string,
check each word ( use space to find words),
whether it's a palindrome or not,
if it is then extract it, else move on to the next word until you got '/0'.
+ 3
Leaky Egg's code has the right idea and almost works. It fails to recognise palindromes followed by punctuation marks though, so the input
Racecar!
would not output any palindromes. An easy fix would be the strip method of string.
+ 1
good Point Tobi.. I'm on it
*Wasn't sure how to go about it, but I found a way. My code now accepts Palendrome even if they have a punctuation. Racecar!
+ 1
Everyone plz checkout my code "Separate Palindromes" and comment about.
0
I don't know c++, but this gave me an idea to kill some time so I wrote a Python script to do this.. Not the language you asked, but maybe it will give you an idea on how it can be done. check my codes