---
title: "Palindrome string using function"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:palindrome-string-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/palindrome-string-function
site: BCT Engineering
---

# Palindrome string using function

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2074 Asoj, 4 marks
- Video: [Check If A String Is A Palindrome | C Programming Example](https://www.youtube.com/watch?v=XMMmvv4wZtQ) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/palindrome-string-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program in C to check whether a given string is palindrome or not using user defined function.

## Short answer

Define a function that compares characters from both ends moving inward. If any pair differs, return 0 (not palindrome); if all match, return1 (palindrome).

## Answer

A palindrome reads the same from both ends (madam, level). The function `isPalindrome()` keeps two indices: `i` at the first character and `j` at the last. While i < j it compares the two characters; if they differ the string is not a palindrome. Otherwise i moves forward and j backward. If all pairs match, it returns 1.

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

int isPalindrome(char s[])
{
    int i = 0, j = strlen(s) - 1;
    while (i < j) {
        if (s[i] != s[j])
            return 0;
        i++;
        j--;
    }
    return 1;
}

int main(void)
{
    char str[100];
    printf("Enter a string: ");
    scanf("%99s", str);
    if (isPalindrome(str))
        printf("%s is a palindrome\n", str);
    else
        printf("%s is not a palindrome\n", str);
    return 0;
}
```

**Sample run**

Input: `madam`

```text
Enter a string: madam is a palindrome
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does isPalindrome() check the string?
   A. Sorts the string and compares
   B. Compares each character with the next one
   C. Compares s[i] and s[j] from both ends and moves inward
   D. Counts the vowels
   Answer: C. i starts at the first character, j at the last, and they move toward each other. (Key point: Two-index method)

2. Why does the while loop run only while i < j?
   A. To avoid reading the first character
   B. Once the indices meet or cross, all pairs have been compared
   C. Because strlen returns n+1
   D. So that the loop runs forever
   Answer: B. After the middle is passed, the remaining comparisons would only repeat earlier ones. (Key point: Loop condition)

3. When does the function return 0?
   A. After the whole loop finishes
   B. When the string length is odd
   C. As soon as a mismatching pair is found
   D. When i equals j
   Answer: C. A single differing pair proves it is not a palindrome, so it returns 0 immediately. (Key point: Return value)

4. What does the function return for 'level'?
   A. 0, because the length is odd
   B. 1, since all pairs match
   C. 0, because it contains repeated letters
   D. It cannot be checked
   Answer: B. l-l and e-e match and the middle v is unpaired, so 1 is returned. (Key point: Palindrome result)

## Related in Computer Programming

- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Check string consists of alphabet only](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-alphabet-check) — Computer Programming, chapter 7
- [Palindrome check in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-palindrome) — Computer Programming, chapter 11
- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [nth Fibonacci term prime check](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afibonacci-nth-prime) — Computer Programming, chapter 6
- [How a function is defined in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-in-c) — Computer Programming, chapter 6

## Related in other subjects

- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
- [Voltage across string discs is not uniform](https://www.bctengineer.com/question.md?key=semester_4%2Fpower_system_analysis_i%3Astring-voltage-not-uniform) — Power System Analysis I, chapter 2, asked 3×
- [ZOH and FOH defined; ZOH transfer function](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Azoh-foh-definition) — Digital Control System, chapter 1
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
