C program – To check whether the given string is palindrome

by Nideesh C on April 18, 2011 · 0 comments

in C Programming





Method 1

#include<stdio.h>
#include<conio.h>
int stpal(char str1[50], char str2[50]);
void main()
{
char str[50], rev[50];
int pal;
clrscr();
printf(“nnt ENTER A STRING…: “);
gets(str);
pal = stpal(str, rev);
printf(“nt THE REVERSED STRING IS…: “);
puts(rev);
if(pal)
printf(“nt THE ENTERED STRING IS A PALINDROME”);
else
printf(“nt THE ENTERED STRING IS NOT A PALINDROME”);
getch();
}
int stpal(char str1[50], char str2[50])
{
int i = 0, len = 0, r = 0, pal = 1;
while(str1[len]!=’′)
len++;
for(i=len-1; i>=0; i–)
{
str2[r] = str1[i];
r++;
}
str2[r] = ‘′;
for(i=0; i<=len-1; i++)
{
if(str1[i] == str2[i])
pal = 1;
else
{
pal = 0;
break;
}
}
return pal;
}

 

Method 2

#include<stdio.h>
#include<conio.h>
int stpal(char str[50]);
void main()
{
char str[50];
int pal;
clrscr();
printf(“nnt ENTER A STRING…: “);
gets(str);
pal = stpal(str);
if(pal)
printf(“nt THE ENTERED STRING IS A PALINDROME”);
else
printf(“nt THE ENTERED STRING IS NOT A PALINDROME”);
getch();
}
int stpal(char str[50])
{
int i = 0, len = 0, pal = 1;
while(str[len]!=’′)
len++;
len–;
for(i=0; i<len/2; i++)
{
if(str[i] == str[len-i])
pal = 1;
else
{
pal = 0;
break;
}
}
return pal;
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program – Copy the contents of one string to another string using pointer
  2. C program – To copy the contents of one string to another string
  3. C program To reverse the given string
  4. C program to read a string and check whether it is a palindrome or not (without using library functions). Output the given string along with suitable message.
  5. C program to find the length of a string without using the built-in function also check whether it is a palindrome or not

Leave a Comment

Previous post:

Next post: