C++ - Palindrome
I have given here C++ program to check whether given string is a palindrome or not
Source Code
// Program to check given string is a palindrome or not
#include <iostream>
#include <string>
int main()
{
while(1)
{
char buf[128];
std::cout << "\nEnter your string or <CTRL+C> to exit: ";
std::cin >> buf;
while(1)
{
int len = strlen(buf);
if( len < 3)
{
std::cout << "\nEnter a string with at least 3 characters: ";
std::cin >> buf;
}
else
break;
}
int len = strlen(buf);
// Make it uppercase
for(int i = 0; i < len; i++)
buf[i] = toupper(buf[i]);
// Check whether the given string is a palindrome or not
bool palindrome = true;
for(int i = 0; i < len / 2 + 1; i++)
{
if(buf[i] != buf[len - i - 1])
{
palindrome = false;
break;
}
}
if(palindrome == true)
std::cout << buf << " is a palindrome\n";
else
std::cout << buf << " is NOT a palindrome\n";
}
return 0;
}
Output
|