×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to check a string is palindrome or not without using library function

Here, we are going to learn how to check a string is palindrome or not without using library function in C programming language?
Submitted by Nidhi, on July 15, 2021

Problem statement

Given a string, we have to check whether the given string is palindrome or not without using library function.

Checking a string is palindrome or not without using library function

The source code to check a string is a palindrome or not without using the library function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

C program to check a string is palindrome or not without using library function

// C program to check a string is palindrome or not
// without using library function

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

int main()
{
    char str[32] = { 0 };
    char rev[32] = { 0 };

    int cnt1 = 0;
    int cnt2 = 0;

    int len = 0;
    int flg = 0;

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

    while (str[cnt1++] != '\0')
        len++;

    //Reverse the string.
    cnt1 = 0;
    cnt2 = len - 1;
    while (cnt2 >= 0)
        rev[cnt1++] = str[cnt2--];

    rev[len] = '\0';

    for (cnt1 = 0; cnt1 < len; cnt1++) {
        if (str[cnt1] != rev[cnt1]) {
            flg = 1;
            break;
        }
    }

    if (flg == 0)
        printf("%s is a palindrome\n", str);
    else
        printf("%s is not a palindrome\n", str);

    return 0;
}

Output

RUN 1:
Enter a string: malayalam
malayalam is a palindrome

RUN 2:
Enter a string: abcdcba
abcdcba is a palindrome

RUN 3:
Enter a string: hello
hello is not a palindrome

Explanation

Here, we read a string str from the user using the scanf() function. Then we reversed the string and compared the string with its reverse to check given string is palindrome or not and print the appropriate message on the console screen.

C String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.