Skip to main content

Command Palette

Search for a command to run...

Java Programming Question

Published
6 min read

def check_palin (str): for i in range (0, int (len (str)/2)): if str [i] != str [len (str) -i-1]: return False return True str_1 = input (“Enter a string.”) ans = check_palin (str_1) if (ans): print (“Yes, it is a palindrome.”) else: print (“No, it is not a palindrome.”)

A string is said to be palindrome if the reverse of the string is the same as the string. In this article, we will learn how to check whether the given string is palindrome or not using C program.

Example

Input*: str = “madam”
Output: “madam” is palindrome.
Explanation: The reverse of “madam” is “madam”. So, it is palindrome*

Input*: str = “hello”
Output: “hello” is not palindrome.
Explanation: The reverse of “hello” is “olleh”. So, it is not palindrome*

Try it on GfG Practice

redirect icon

In C, there are mainly two different ways to check whether the string is palindrome or not:

Table of Content

Checking for a palindrome in C is a great exercise for string manipulation. To further explore string operations and data structures, the C Programming Course Online with Data Structures provides step-by-step guides on string handling and other common algorithms.

By Reversing String

The idea is to reverse the given string and store it in a temporary array. Then compare both of them using strcmp() function. If they are equal, then the string is palindrome, otherwise, it is not.

Code Implementation

C1

// Program to check for a palindrome string by reversing

2

// the string

3

#include <stdio.h>

4

#include <string.h>

5

#include <stdbool.h>

6

#include <stdlib.h>

7

8

char *strrev(char *str) {

9

    int len = strlen(str);

10

11

      // Temporary char array to store the

12

    // reversed string

13

    char *rev = (char *)malloc

14

      (sizeof(char) * (len + 1));

15

16

    // Reversing the string

17

    for (int i = 0; i < len; i++) {

18

        rev[i] = str[len - i - 1];

19

    }

20

    rev[len] = '\0';

21

    return rev;

22

}

23

24

void isPalindrome(char *str) {

25

26

    // Reversing the string

27

    char *rev = strrev(str);

28

29

    // Check if the original and reversed

30

      // strings are equal

31

    if (strcmp(str, rev) == 0)

32

        printf("\"%s\" is palindrome.\n",

33

               str);

34

    else

35

        printf("\"%s\" is not palindrome.\n",

36

               str);

37

}

38

39

int main() {

40

41

      // Cheking for palindrome strings

42

    isPalindrome("madam");

43

      isPalindrome("hello");

44

45

    return 0;

46

}

Output

"madam" is palindrome.
"hello" is not palindrome.

Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), for storing the reversed string.

By Using Two Pointers

In this method, two index pointers are taken: one pointing to the first character and other pointing to the last character in the string. The idea is to traverse the string in forward and backward directions simultaneously while comparing characters at the same distance from start and end.

  • If a pair of distinct characters is found, then the string is not palindrome.

  • If the two pointers meet at the middle of the string without any mismatched characters, then it is palindrome.

Check-for-Palindromic-String-2.webp

Check-for-Palindromic-String-2.webp

2 / 4

C1

// Program to check for a palindrome string

2

// using two pointers

3

#include <stdio.h>

4

#include <string.h>

5

#include <stdbool.h>

6

7

void isPalindrome(char *str) {

8

9

      // Index pointer to the start

10

    int left = 0;

11

12

    // Index pointer to the end

13

    int right = strlen(str) - 1;

14

15

    // Run the loop till both pointer

16

    // meet

17

    while (left < right) {

18

19

        // If characters don't match,

20

        // string is not palindrome

21

        if (str[left] != str[right]) {

22

            printf("\"%s\" is not palindrome.\n",

23

               str);

24

            return;

25

        }

26

27

        // Move both pointers towards

28

        // each other

29

        left++;

30

        right--;

31

    }

32

33

    // If all characters match,

34

    // string is palindrome

35

    printf("\"%s\" is palindrome.\n",

36

               str);

37

}

38

39

int main() {

40

41

      // Checking if given strings are palindrome

42

      isPalindrome("madam");

43

      isPalindrome("hello");

44

      return 0;

45

}

Output

"madam" is palindrome.
"hello" is not palindrome.

Time complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(1)

By Using Recursion

Two-pointer approach can also be implement using recursion. The current first and last index pointers (representing the current first and last characters) can be passed to the function as arguments.

  • If the characters at these pointers match, the function increments first pointer, decrements second pointer and then call itself again with updated pointers.

  • Otherwise, it returns false as the string is not palindrome.

  • If all the characters match till the first pointer is less than the last pointer, the string is palindrome so return true.

Code Implementation

C1

// Program to check for a palindrome string

2

// using a recursive approach

3

#include <stdio.h>

4

#include <string.h>

5

#include <stdbool.h>

6

7

// Recursive helper function to check if the string

8

// is palindrome

9

bool palinHelper(char *str, int left, int right) {

10

11

    // If the start and end pointers cross

12

    // each other, it means all characters

13

    // have matched

14

    if (left >= right)

15

        return true;

16

17

    // If characters don't match,

18

    // string is not palindrome

19

    if (str[left] != str[right])

20

        return false;

21

22

    // Recursively check for the rest of

23

    // the string

24

    return palinHelper(str, left + 1, right - 1);

25

}

26

27

void isPalindrome(char* str) {

28

29

      // Calling the recursive function to check

30

    // palindrome string

31

    if (palinHelper(str, 0, strlen(str) - 1))

32

        printf("\"%s\" is palindrome.\n", str);

33

    else

34

        printf("\"%s\" is not palindrome.\n", str);

35

}

36

37

int main() {

38

39

    // Checking if the given strings are palindrome

40

    isPalindrome("madam");

41

    isPalindrome("hello");

42

    return 0;

43

}

Output

"madam" is palindrome.
"hello" is not palindrome.

Time complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(n), due to recursive stack space.

More from this blog

Development of net banking application

18 posts