C Books Guide and List
C++ Books Guide and List
Best Java Books

Write a program to check whether the given string is a palindrome.

0 votes
2,902 views
Write a program to check whether the given string is a palindrome.
asked Jul 19, 2011 by sagar Expert (12,340 points)

2 Answers

0 votes
 
Best answer
Palindrome is a string, which when read in both forward and backward way is same.
Example: radar, madam, pop, lol, etc.

Program:
#include <stdio.h>
#include <string.h>
int main()
{
 char string1[20];
 int i, length;
 int flag = 0;
 printf("Enter a string: \n");
 scanf("%s", string1);
 length = strlen(string1);
 for(i=0;i < length ;i++)
{
  if(string1[i] != string1[length-i-1])
{
   flag = 1;
   break;
  }
 }
 if (flag)
{
  printf("%s is not a palindrome\n", string1);
 }
 else
{
  printf("%s is a palindrome\n", string1);
 }
 return 0;
}

Output:
Enter a string: radar
"radar" is a palindrome


Explanation :

To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.
Consider a palindrome string: "radar",
---------------------------
index: 0 1 2 3 4
value: r a d a r
---------------------------
To compare it with the reverse of itself, the following logic is used:
0th character in the char array, string1 is same as 4th character in the same string.
1st character is same as 3rd character.
2nd character is same as 2nd character.
. . . .
ith character is same as 'length-i-1'th character.
If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome.
By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.
answered Jul 19, 2011 by sagar Expert (12,340 points)
0 votes

Palindrome is a string, which when read in both forward and backward way is same.

Example: radar, madam, pop, lol, rubber, etc.,

Program:

#include <stdio.h>

#include <string.h>

int main() {

char string1[20];

int i, length;

int flag = 0;

printf("Enter a string: \n");

scanf("%s", string1);

length = strlen(string1);

for(i=0;i < length ;i++){

if(string1[i] != string1[length-i-1]){

flag = 1;

break;

}

}

if (flag) {

printf("%s is not a palindrome\n", string1);

}

else {

printf("%s is a palindrome\n", string1);

}

return 0;

}

Output:

Enter a string: radar

"radar" is a palindrome

Explanation with example:

To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.

Consider a palindrome string: "radar",

---------------------------

index: 0 1 2 3 4

value: r a d a r

---------------------------

To compare it with the reverse of itself, the following logic is used:

0th character in the char array, string1 is same as 4th character in the same string.

1st character is same as 3rd character.

2nd character is same as 2nd character.

. . . .

ith character is same as 'length-i-1th character.

If any one of the above condition fails, flag is set to true(1), which implies that the string is not a

palindrome. By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.

answered Jan 8 by keem Expert (13,240 points)

Related questions

0 votes
2 answers 618 views
+2 votes
4 answers 1,276 views
1,276 views asked Jul 13, 2011 by Smita Advisor (7,140 points)
0 votes
1 answer 3 views
...