diff --git a/C++/Palindrome.cpp b/C++/Palindrome.cpp new file mode 100644 index 00000000..d2f58e3c --- /dev/null +++ b/C++/Palindrome.cpp @@ -0,0 +1,30 @@ +#include +#include + +// A function to check if a string str is palindrome +void isPalindrome(char str[]) +{ + // Start from leftmost and rightmost corners of str + int l = 0; + int h = strlen(str) - 1; + + // Keep comparing characters while they are same + while (h > l) + { + if (str[l++] != str[h--]) + { + printf("%s is not a palindrome\n", str); + return; + } + } + printf("%s is a palindrome\n", str); +} + +// Driver program to test above function +int main() +{ + isPalindrome("abba"); + isPalindrome("abbccbba"); + isPalindrome("geeks"); + return 0; +}