Write a program to check whether a given sentence (ending with '.') is a palindrome or not. Without space. Using function in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a program to check whether a given sentence (ending with '.') is a palindrome or not. Without space. Using function in c++

Write a program to check whether a given sentence (ending with '.') is a palindrome or not. The checking shall be carried out in a function to be called ispalindrome that takes as input the input sentence. ispalindrome shall ignore all spaces and shall return either true or false. ppplllzzzzz I need someone to solve it as soon as possible.

20th Apr 2018, 3:31 PM
Tamii Mohammad
Tamii Mohammad - avatar
5 Answers
+ 3
inline bool is_palindrome(const std::string& sentence) { std::string s; for (const char c : sentence) if (!std::isspace(c)) s.push_back(/*std::tolower(*/c/*)*/); const size_t length = s.length() - 1; //Note the - 1: It discards the ending point. const size_t mid = length / 2; for ( size_t i = 0, ri = length-1; i != mid; ++i, --ri ) { if(s[i] != s[ri]) return false; } return true; // If the loop didn't find a mismatch, s is a palindrome. }
20th Apr 2018, 3:53 PM
Timon Paßlick
+ 1
Should ‘.’ count as a symbol of the sentence? Do we need to pay attention to the register? Ok, here’s the solution ignoring the dots and for the case where all letters are lower-case: bool isPalindrome (string s) { string clear = “”; for (int i = 0; i < s.lengh(); i++) { if (s[i] != ‘ ‘ && s != ‘.’) clear += s[i]; for (int i = 0; i < clear.length(); i++) if (clear[i] != clear[clear.length() - i - 1]) return false; return true; }
20th Apr 2018, 3:51 PM
Ali Zhussupov
Ali Zhussupov - avatar
+ 1
This is just the palindrome part of the code. bool palindrome(std::string str) { for(unsigned i = 0, j = str.size()-1; i < str.size(); ++i, --j) if(str[i] != str[j]) return false; return true; } EDIT: Didnt realize Timon has same code.
20th Apr 2018, 4:27 PM
Bebida Roja
Bebida Roja - avatar
+ 1
Bebida Roja It's okay if it was unintentional. ( :
20th Apr 2018, 4:37 PM
Timon Paßlick
0
thank you so much guys..🌺🌺🕊🕊🕊
20th Apr 2018, 4:05 PM
Tamii Mohammad
Tamii Mohammad - avatar