Write a function that takes a string as input and determines whether it is a palindrome or not.
Program in C
C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool isPalindrome(const char* str) {
int length = strlen(str);
int i, j;
for (i = 0, j = length - 1; i < j; i++, j--) {
if (str[i] != str[j]) {
return false;
}
}
return true;
}
int main() {
const char* input = "madam";
if (isPalindrome(input)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
Explanation:
- The function isPalindrome takes a const char* parameter str, representing the input string, and returns a boolean value (true if the string is a palindrome, and false otherwise).
- The variable length is assigned the length of the input string using the strlen function from the <string.h> library.
- Two integer variables i and j are declared. i starts from the beginning of the string (0 index), and j starts from the end of the string (length – 1 index).
- The for loop iterates through the string. The loop continues as long as i is less than j. In each iteration, i is incremented and j is decremented.
- Inside the loop, the characters at positions i and j are compared. If they are not equal, the function immediately returns false, indicating that the string is not a palindrome.
- If the loop completes without finding any unequal characters, the function returns true, indicating that the string is a palindrome.
- In the main function, a sample input string “madam” is defined and passed to the isPalindrome function.
- The function’s return value is checked using an if statement. If it is true, the program prints “The string is a palindrome.” Otherwise, it prints “The string is not a palindrome.”
- Finally, the main function returns 0, indicating successful execution of the program.
C Output
The string is a palindrome.
Program in Java
Java
public class PalindromeChecker {
public static boolean isPalindrome(String str) {
int length = str.length();
int i, j;
for (i = 0, j = length - 1; i < j; i++, j--) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String input = "jahaj";
if (isPalindrome(input)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}
Explanation:
- The isPalindrome method takes a String parameter str and returns a boolean value indicating whether the string is a palindrome (true) or not (false).
- The method calculates the length of the input string using the length() method of the String class.
- Two integer variables i and j are declared. i starts from the beginning of the string (0 index), and j starts from the end of the string (length – 1 index).
- The for loop iterates through the string. It continues as long as i is less than j. In each iteration, i is incremented and j is decremented.
- Inside the loop, the characters at positions i and j are compared using the charAt() method of the String class. If they are not equal, the method immediately returns false, indicating that the string is not a palindrome.
- If the loop completes without finding any unequal characters, the method returns true, indicating that the string is a palindrome.
- In the main method, a sample input string “jahaj” is assigned to the input variable.
- The isPalindrome method is called with the input string. The return value is checked using an if statement. If it is true, the program prints “The string is a palindrome.” Otherwise, it prints “The string is not a palindrome.”
Java Output
The string is a palindrome.