Q:

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

belongs to collection: C String Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

Program:

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

#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to check a string is palindrome or not u... >>
<< C program to compare strings using strcmp() functi...