Certainly! Here’s an example of a palindrome program in C:
#include <stdio.h> #include <string.h> int checkPalindrome(char str[]) { int len = strlen(str); int i, j; for (i = 0, j = len - 1; i < j; i++, j--) { if (str[i] != str[j]) { return 0; // Not a palindrome } } return 1; // Palindrome } int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Remove newline character from input str[strcspn(str, "\n")] = '\0'; if (checkPalindrome(str)) { printf("The string is a palindrome.\n"); } else { printf("The string is not a palindrome.\n"); } return 0; }
In this program, we define a function checkPalindrome
that takes a string as input and checks if it is a palindrome. It uses two pointers, i
and j
, starting from the beginning and end of the string respectively, and compares the characters at these positions. If at any point the characters don’t match, the function returns 0 indicating that the string is not a palindrome. Otherwise, if the loop completes without finding any mismatch, the function returns 1 indicating that the string is a palindrome.
In the main
function, we read a string from the user using fgets
and remove the newline character at the end. Then, we call the checkPalindrome
function and display an appropriate message based on the result.
Note that the program assumes a maximum string length of 100 characters. You can adjust this value as per your requirements.
Palindrome number algorithm:
To determine whether a number is a palindrome, you can use the following algorithm:
- Take the input number.
- Create a temporary variable and set it equal to the input number.
- Initialize a variable to store the reverse of the number, initially set to 0.
- While the temporary variable is greater than 0, perform the following steps:
- Extract the last digit of the temporary variable using the modulus operator
%
. - Multiply the reverse variable by 10 and add the extracted digit to it.
- Divide the temporary variable by 10, discarding the last digit.
- Extract the last digit of the temporary variable using the modulus operator
- After the loop, compare the reverse variable with the original input number.
- If they are equal, the number is a palindrome.
- Otherwise, the number is not a palindrome.
Here’s an example implementation of the palindrome number algorithm in C:
#include <stdio.h> int isPalindrome(int num) { int temp = num; int reverse = 0; while (temp > 0) { int digit = temp % 10; reverse = reverse * 10 + digit; temp /= 10; } return (num == reverse); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isPalindrome(num)) { printf("The number is a palindrome.\n"); } else { printf("The number is not a palindrome.\n"); } return 0; } #include <stdio.h> int isPalindrome(int num) { int temp = num; int reverse = 0; while (temp > 0) { int digit = temp % 10; reverse = reverse * 10 + digit; temp /= 10; } return (num == reverse); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isPalindrome(num)) { printf("The number is a palindrome.\n"); } else { printf("The number is not a palindrome.\n"); } return 0; }
In this program, the isPalindrome
function takes an integer num
as input and checks whether it is a palindrome. It initializes a temporary variable temp
with the input number and a reverse
variable to store the reverse of the number.
The algorithm loops until the temporary variable becomes zero. In each iteration, it extracts the last digit of the temporary variable using the modulus operator %
, multiplies the reverse
variable by 10, and adds the extracted digit to it. Then, it divides the temporary variable by 10 to remove the last digit.
After the loop, the function compares the original input number with the reversed number. If they are equal, it returns 1
indicating that the number is a palindrome. Otherwise, it returns 0
indicating that the number is not a palindrome.
In the main
function, we read a number from the user using scanf
and call the isPalindrome
function to determine if it is a palindrome. Finally, we display an appropriate message based on the result.