Program 86:
#include<stdio.h>
#include<string.h>
main()
{
int i,j=0;
char str1[100],str2[100]={0};
printf("Enter a string to know whether it is palindrome or not\n");
gets(str1);
for(i=strlen(str1)-1;i>=0;i--)
{
str2[j]=str1[i];
j++;
}
str2[j]='\0';
printf("The reverse of the given string is---> %s\n",str2);
if(strcmp(str1,str2)==0)
{
printf("The given string is Palindrome\n");
}
else
{
printf("The given string is Not Palindrome\n");
}
}
Explanation:- This program starts with initializing :
- i,j → used as helping variable
- str1 → To store input from user
- str2 → To store output of program
- i,j → used as helping variable
printf("Enter a string to know whether it is palindrome or not\n"); gets(str1);Taking input from userfor(i=strlen(str1)-1;i>=0;i--) { str2[j]=str1[i]; j++; }- Lets take string example LOL.
- Iteration 1: i=strlen(str1)-1→3-1→i=2 ;2>=0 so loop is executed.j=0
- str2[j]=str1[i]→str2[0]=str1[0]→str2[0]=L
- j++→j=1
- i-- →i=1
- Iteration 2:1>=0 so loop is executed.
- str2[j]=str1[i]→str2[1]=str1[1]→str2[1]=O
- j++→j=2
- i--→i=0
- Iteration 3:0>=0 so loop is executed.
- str2[j]=str1[i]→str2[2]=str1[0]→str2[2]=L
- j++→j=3
- i--→i=-1
- Iteration 4:-1 is not greater than or equal to 0 so loop is terminated.
- str2[j]='\0' To indicated the end of the string
if(strcmp(str1,str2)==0) { printf("The given string is Palindrome\n"); } else { printf("The given string is Not Palindrome\n"); }compares str1 and str2 where str1=LOL and str2=LOL so strcmp(str1,str2) returns 0 which is equal to 0 so the if part is executed where it prints The given string is Palindrome




