ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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 Solution:

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

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.