#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s[101], pal[101];
cin.getline(s,100);
for (int i = strlen(s); i >= 0; i--)
strcat(pal, s[i]);
if (strcmp(s, pal) == 0)
cout << "corect";
else
cout << "incorect";
return 0;
}
Hello. I tried to verify if a word is a palindrome and it gave me the error:
"Invalid conversion from 'char' to 'const char*' [-fpermissive]".
It isn't the first time when I encounter this annoying error and I need some help. Can someone explain what's wrong with my code and (please :)) ) give me some article/theory/something to understand why this error occurs? Thank you and sorry for asking such silly question. I saw it like 1000 times and still don't understand it.
As has already been pointed out the reason you are getting the error is because you are passing a char
and not a const char*
to strcat
.
As the question is marked as c++
I'll go ahead and show the c++-way of doing this.
#include <iostream>
#include <string>
int main() {
std::string s, pal;
std::getline(std::cin, s);
for (auto it = s.rbegin(), end = s.rend(); it != end; ++it)
pal.push_back(*it);
if (s == pal)
std::cout << "correct" << std::endl;
else
std::cout << "incorrect" << std::endl;
return 0;
}
Prefer std::string to using raw c-style char arrays, and with is comes the use of a reverse_iterator in the loop.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments