From a7c4e8a6310188a687db046f89a850a5795f3239 Mon Sep 17 00:00:00 2001 From: Premashish <198003@nith.ac.in> Date: Sat, 2 Oct 2021 15:28:56 +0530 Subject: [PATCH] Added Palindrome.cpp Added Program to check if a entered string is palindrome or not. --- C++/Palindrome.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 C++/Palindrome.cpp 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; +}