Palindrome Program in C Using Functions and Pointers

Created with Sketch.

Palindrome Program in C Using Functions and Pointers

Introduction

In this blog post, we will explore the concept of palindromes and discuss how to write a C program to check whether a given string is a palindrome using functions and pointers. Palindromes are sequences of characters that read the same forward as backward. We’ll delve into the algorithm for palindrome checking, provide step-by-step explanations, present C code using functions and pointers, and include examples with outputs.

Understanding Palindromes

A palindrome is a sequence of characters that remains the same when read forward or backward. Palindromes can be words, phrases, numbers, or other sequences of characters. Examples of palindromic words include “radar,” “level,” and “civic.” Palindromes exhibit symmetry in their character arrangement.

Algorithm for Palindrome Checking Using Functions and Pointers

The algorithm for checking whether a given string is a palindrome using functions and pointers involves using two pointers to iterate through the string from both ends. By employing functions, we encapsulate the palindrome-checking logic for better modularity. Here’s a step-by-step breakdown of the algorithm:

  1. Input: Receive the string to be checked for palindromicity.

  2. Initialization: Set two pointers, one at the beginning and one at the end of the string.

  3. Comparison: Compare the characters at the pointers. If they are equal, move the pointers towards each other. If they are not equal, the input is not a palindrome.

  4. Repeat: Repeat the comparison and pointer movement until the pointers meet in the middle.

  5. Palindrome Check: If the pointers meet, the input is a palindrome; otherwise, it is not.

Palindrome Program in C Using Functions and Pointers

Now, let’s implement the palindrome-checking algorithm in a C program using functions and pointers.

#include <stdio.h>
#include <string.h>

// Function to check if a given string is a palindrome
int isPalindrome(char *str) {
    char *start = str;
    char *end = str + strlen(str) - 1;

    // Check for palindrome
    while (start < end) {
        if (*start != *end) {
            return 0; // Not a palindrome
        }
        start++;
        end--;
    }

    return 1; // Palindrome
}

int main() {
    char inputString[100];

    printf("Enter a string: ");
    scanf("%s", inputString);

    if (isPalindrome(inputString)) {
        printf("%s is a palindrome.\n", inputString);
    } else {
        printf("%s is not a palindrome.\n", inputString);
    }

    return 0;
}

Output Example

Example: Palindrome String Using Functions and Pointers

Enter a string: madam
madam is a palindrome.

Explanation

This program defines a function isPalindrome that checks whether a given string is a palindrome. The function takes a pointer to the string as an argument, allowing it to work with different strings. The pointers start and end are used to iterate through the string from both ends. Characters are compared until the pointers meet in the middle.

Conclusion

Using functions and pointers in C programs adds modularity and flexibility to the code. In the context of palindrome checking, this approach allows us to encapsulate the palindrome logic in a function, making the code more readable and maintainable.

Feel free to experiment with the provided C code in your C development environment. If you have any questions, encounter issues, or want to explore more about palindromes, C programming, or the use of functions and pointers, don’t hesitate to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *