0
1.6kviews
Write a program to validate whether accepted string is palindrome or not.
1 Answer
0
12views
  • Palindrome can be a word, phrase, or sequence that reads the same backwards as forwards.

    • Some palindrome strings examples are "dad", "radar", "madam" etc.

    • Palindrome program works as follows:- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string.

    • If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. [Note: If this question is asked for 10 marks then write algorithm]

Algorithm:

  • Start.

  • Input a String.

  • Initialize Len to zero, Flag to zero.

  • While String [Len] is not equal to NULL.

  • Copy the string.

  • Reverse the string.

  • Now compare both strings.

  • If it’s equal to zero.

  • Print String Is a Palindrome.

  • Else.

  • Print String Is Not a Palindrome.

  • Stop.

Program:

#include <stdio.h>
#include <string.h>
int main(){
    char a[100], b[100];
    printf("Enter the string to check if it is a palindrome:\n");
    gets(a);

    strcpy(b,a);
    strrev(b);

    if (strcmp(a,b) == 0)
        printf("Entered string is a palindrome.\n");
    else
        printf("Entered string is not a palindrome.\n");
    return 0;
}

Output:

Enter the string to check if it is a palindrome: nayan

Entered string is a palindrome.

Enter the string to check if it is a palindrome: sagar

Entered string is not a palindrome.

Please log in to add an answer.