I tried to find a solution on internet but couldnt find anything similar to this. I am using strcpy and iteration to make a palindrome function in c++ everything is working fine but the strcpy section. I dont know how to solve it or what other alternative to use. Thank you.
#include <iostream>
#include <cstring>
using namespace std;
void palindrom(char[]);
int main()
{
char binput[100];
cout << "Hello please enter your word here: " << endl;
cin >> binput;
palindrom(binput);
system("pause");
return 1;
}
void palindrom(char binput[])
{
int max= strlen(binput);
char cinput[100];
char dinput[100];
for (int i=max, n=0; i>=0, n<=max; i--, n++)
strcpy(dinput[n],binput[i]);
cout << dinput << endl;
if (strcmp(binput,dinput)==true)
cout << "Is palindrome " << endl;
else
cout << "Is not " << endl;
}
Hope this solves.Basically first just check the first letter of the word and the last. If they are not equal then they are not palindrome. If they are equal then proceed on by comparing from the front end character with their respective back ends.
#include<iostream>
#include<cstring>
using namespace std;
int CheckPalindrome(char input[],int len);
int main()
{
char input[100];
int result,inpLen;
cout<<"Enter Word:"<<endl;
cin>>input;
cout<<"Entered Word:"<<input<<endl;
cout<<"Checking....."<<endl;
inpLen=strlen(input);
result=CheckPalindrome(input,inpLen);
if(result == 1)
{
cout<<"Entered Word:"<<input<<" is a palindrome!"<<endl;
}
else
{
cout<<"Entered Word:"<<input<<" is not a palindrome!"<<endl;
}
return 0;
}
int CheckPalindrome(char input[],int len)
{
int result;
if(input[0] != input[len-1])
{
result = 0;
}
else
{
for(int i=0 ; i<len ; i++)
{
if(input[i] == input[len-1-i])
{
result = 1;
}
else
{
result = 0;
break;
}
}
}
return result;
}