Palindrome String program in C using function

Here we are going to write a program to check palindrome string in c using function. Previously we have done this program normally that is just a c program to check palindrome string. This time we will do this using function.

How to check a palindrome string?

A string is said to be palindrome if the reverse of it is equal to the original string.

Example - madam, malayalam, rar, ma'am, wow, noon, racecar, level, tenet, hannah etc.

Lets discuss about the approach now.

First we will create a function named palstrchk() of void return type that is it won't return anything and it will take one string as parameter. The string which we are sending as parameter is the string which we want to check for Palindrome. Then within that function we will print whether the given string is Palindrome or not. Then in main() function we will call the palstrchk() function, that's it. Now lets code it.

Palindrome String program in C using function

#include <stdio.h>
#include <stdbool.h>

void palstrchk (char arr[]){
     
    bool ch=true;
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=arr[n-1-i])
        {
            ch=false;
            break;
        }
    }
    if(ch==true)
    {
        printf("%s is a palindrome string",arr);
    }
    else
    {
        printf("%s is not a palindrome string",arr);
    }
}

int main(){
    int n;
    printf("Enter the length of string : ");
    scanf("%d",&n);
    char arr[n+1];
    printf("Enter string : ");
    scanf("%s",&arr);
    palstrchk (arr);
    return 0;
}


Output

Enter the length of string : 5
Enter string : madam
madam is a palindrome string

Enter the length of string : 5
Enter string : freak
freak is not a palindrome string


More like this 

• Fibonacci Series in C using function

• Armstrong Number in C using function

• Prime Number check in C using function

• Palindrome Number in C using function 

• Add two numbers in C using function 

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post