A palindrome is just a word whose first character is equal to the last one, and so on. So, in order to check if it is a palindrome, you only need the copy the functionality of the strlen function to know the position of the character you have to compare the first character.
In C++, using your variables, this can be easily done with a while loop:
int i = 0;
// Is this a null terminating character?
while (y[i])
{
// Move to the next character
i++;
}
To make things easier, and truly be a drop-in for strlen, this could be put in a function:
int stringLength (char *input)
{
int i = 0;
while (input[i])
{
i++;
}
return i;
}
Now you just have to loop through the input, comparing the first character to the last, comparing the second character to the second last, and so on... You just have to remember that due to the way arrays work, the last character is actually at position len-1.
#include <iostream> // required for cout, and cin
// returns the length of a c style string
int stringLength(char *input)
{
int i = 0;
// Is this a null terminating character
while (input[i])
{
// No, check the next character
i++;
}
return i;
}
int main()
{
// Get input from the user.
char input[100];
std::cin >> input;
// Calculate the length of the input
int length = stringLength(input);
// At position length is the null terminating character,
// the last character is actually at position len - 1
int lastIndex = length - 1;
// Stores whether of not we found a palindrome
bool isPalindrome = true;
// Loop through the string checking if the first character is equal to
// the last, second to second last etc...
for (int i = lastIndex; i >= length/2; i--)
{
// Check the palindrome condition
if (input[i] != input[lastIndex - i])
{
isPalindrome = false;
break;
}
}
// Output the result
if (isPalindrome)
{
std::cout << "Palindrome" << std::endl;
}
else
{
std::cout << "Not palindrome" << std::endl;
}
return 0;
}