How to "extract palindrome words from sentence"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to "extract palindrome words from sentence"?

5th Apr 2017, 5:39 PM
Aytac
Aytac - avatar
6 Answers
+ 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
5th Apr 2017, 8:59 PM
Squidy
Squidy - avatar
+ 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'.
5th Apr 2017, 7:09 PM
Nikhil Dhama
Nikhil Dhama - avatar
+ 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.
5th Apr 2017, 7:33 PM
Tob
Tob - avatar
+ 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!
5th Apr 2017, 7:40 PM
LordHill
LordHill - avatar
+ 1
Everyone plz checkout my code "Separate Palindromes" and comment about.
6th Apr 2017, 8:18 PM
GeekyShacklebolt
GeekyShacklebolt - avatar
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
5th Apr 2017, 6:18 PM
LordHill
LordHill - avatar