Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a function to detect palindromes in strings

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:

  1. 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).
  2. The variable length is assigned the length of the input string using the strlen function from the <string.h> library.
  3. 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).
  4. 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.
  5. 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.
  6. If the loop completes without finding any unequal characters, the function returns true, indicating that the string is a palindrome.
  7. In the main function, a sample input string “madam” is defined and passed to the isPalindrome function.
  8. 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.”
  9. 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:

  1. The isPalindrome method takes a String parameter str and returns a boolean value indicating whether the string is a palindrome (true) or not (false).
  2. The method calculates the length of the input string using the length() method of the String class.
  3. 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).
  4. 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.
  5. 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.
  6. If the loop completes without finding any unequal characters, the method returns true, indicating that the string is a palindrome.
  7. In the main method, a sample input string “jahaj” is assigned to the input variable.
  8. 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.