Tuesday, May 28, 2019

C Programming : Program to check whether a given string is palindrome or not.

/* Program to check whether a given string is palindrome or not without using string function */

#include<stdio.h>                                                 // header file
#include<conio.h>                                                 // header file
#include<string.h>                                                // header file
int main( )
{
char name[50];                                       // variable declaration
int i,length,len;                                       // variable declaration
printf("\n Enter the string ");
gets(name);
len=strlen(name); 
                            // calculating the length of the  given string
for(i=0;i<len/2;i++)                                    // looping statement
{
if(name[i]!=name[ len-1-i ])             // conditional statement
{
printf("\n Not a palindrome ");                // printing message
break;
}}
if( i==len/2)                                         // conditional statement
printf("\n It is Palindrome ");                   // printing message
getch( );
}

Output

Enter the string
mayanmar
Not a palindrome


Enter the string
malymylam
 It is Palindrome
The Output will be shown as given below:



In this program we are checking the given string is palindrome or not without using the strcmp( ), strcpy( ),and strrev( ) function.
Firstly, we use the header file which replaces the necessary files into the source code file.
Then we declare the variable and printing the message to enter the string.
Then we calculate the length of the given string and then conditional statement is used. If the condition is true then the first message is printed and if the conditional statement is false then the control statement executes the  second conditional statement. The output is shown above:
In the first statement , the entered statement is not a palindrome and  in the second statement, the entered statement is palindrome.

No comments:

Post a Comment

Function

FUNCTION C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or...