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.
5 odpowiedzi
+ 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.
}
+ 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;
}
+ 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.
+ 1
Bebida Roja It's okay if it was unintentional. ( :
0
thank you so much guys..🌺🌺🕊🕊🕊