C program string palindrome

devquora
devquora

Posted On: Jan 26, 2023

C program string palindrome

 

Overview:

In this article First, I want to explain What is a string? Then, I will explain What do you mean by Reverse? and what is positive integer? Moreover, I will explain What is a palindrome? Moreover, I will demonstrate how to check a number is palindrome or not? After all, I will write the logic of a C to check number is palindrome and explain it with output.

Table of contents:

  1. What is a string?
  2. What do you mean by Reverse?
  3. What is palindrome string?
  4. Demonstration to check a string is it palindrome or not.
  5. Logic to check if a string is palindrome or not
  6. C program to check string is palindrome
  7. Explanation of the C program to check string is palindrome with output.
  8. Conclusion

What is a String?

The String is a combination of characters that may be any digit from 0 to 9 and Alphabets from A to Z. The repetition of characters does not matter. For instance, a String Conax uses 5 characters i.e. C,o,n,e,and x.

What do you mean by Reverse?

Reverse means move backward and turned toward the direction opposite to that previously stated. For instance, If our number is 321 then its reverse string is 123.

What is a Palindrome string?

A palindrome string is one that reads the same backward as well as forward. It can be of odd or even length. In other words, A Palindrome is an approach in which the reversed string is the same as the previous string. For instance, the Reverse of a number 323 is also 323. So we can say that 323 is a palindrome.

Demonstration to check a string is it palindrome or not

Demo of palindrome string

Logic to check a string, is it palindrome or not

  • Step 1: Copy input string into a new string (strcpy function).
  • Step 2: Reverse the character of it.
  • Step 3: Compare it with the original string (strcmp function).
  • Step 4: If both of them have the same sequence of characters, i.e., they are identical, then the string is a palindrome otherwise not.

C program string palindrome

#include <stdio.h>
#include <string.h>
int main()
{
  char a[100], b[100];
  while(1){

  printf("Plese! Enter a string to check if it's a palindrome:\n");
  gets(a);

  strcpy(b, a);  // Copying input string
  strrev(b);  // Reversing the string

  if (strcmp(a, b) == 0)  // Comparing input string with the reverse string
    printf("The string is a palindrome.\n");
  else
    printf("The string isn't a palindrome.\n");
   }
  return 0;
 
}

The output of the Program with code:

palindrome string

 

Conclusion: Explanation of the C program to reverse a string with output.

In the successful compilation of the program, a message is displayed on the screen as Please! Enter a string to check if it's a palindrome:

This program entered a string by user then store it into a variable after that found its reversed and store it in to another variable if both variables are same then we can say that inputed string is palindrome otherwise that is not a palindrome.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..