C program to check whether a string is palindrome or not

Write a C program to check whether a string is palindrome or not without using loop. How to check whether a string is palindromic string or not without using string library functions in C programming.

Write a C program to check whether a string is palindrome or not using inbuilt string library functions. C program to check palindrome string using string library functions.

Example:
Input String: madam
Output: Palindrome string

Required knowledge

Basic C programming, Loop, If else, String

Palindromic string

Palindrome string is a special string which reads same from backward or forward such as madam, mom, eye, dad etc.

Logic to check palindrome string

The basic idea behind checking palindrome is if it can be read same from forward and backward then it is palindrome else not. Here in the below algorithm we will traverse the string character by character in both direction at the same time if they are equal then the string is palindrome.
Algorithm to check Palindrome string
%%Input : text {Array of characters /String)
        N {Size of the string}
Begin:
    S ← 0; EN-1;
    While (S <= E) do
        If (text[S] != text[E])
            break
        End if
        SS + 1;
        EE - 1;
    End while
    If (S >= E) then
        write ('String is Palindrome')
    End if
    Else then
        write ('String is Not Palindrome')
    End if
End


Program to check palindrome string

/**
 * C program to check whether a string is palindrome or not without using string functions
 */

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the string

int main()
{
    char string[MAX_SIZE];
    int len, S, E;

    /* Reads a string from user */
    printf("Enter any string: ");
    gets(string);


    /* Finds length of the string */
    len = 0;
    while(string[len]!='\0')
    {
        len++;
    }

    S = 0;
    E = len-1;
    
    /* Finds if string is equal to its reverse */
    while(S <= E)
    {
        if(string[S] != string[E])
        {
            break;
        }

        S++;
        E--;
    }

    if(S >= E)
    {
        printf("String is Palindrome.\n");
    }
    else
    {
        printf("String is Not Palindrome.\n");
    }

    return 0;
}


Note: You can also use inbuilt string library functions to make the task easier. Using inbuilt string library function you just need to find reverse of the string and then compare it with original string.


Program to check palindrome string using string functions

/**
 * C program to check whether a string is palindrome or not using string functions
 */

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

#define MAX_SIZE 100 //Maximum size of the string

int main()
{
    char string[MAX_SIZE], reverse[MAX_SIZE];
    int flag;

    /* Reads a string from user */
    printf("Enter any string: ");
    gets(string);


    strcpy(reverse, string); //Copies original string to reverse
    strrev(reverse); //Finds the reverse of string

    flag = strcmp(string, reverse); //Checks whether both are equal or not

    /* If both strings are equal */
    if(flag == 0)
    {
        printf("String is Palindrome.\n");
    }
    else
    {
        printf("String is Not Palindrome.\n");
    }

    return 0;
}


Output
Enter any string: madam
String is Palindrome.


Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment