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:
- What is a string?
- What do you mean by Reverse?
- What is palindrome string?
- Demonstration to check a string is it palindrome or not.
- Logic to check if a string is palindrome or not
- C program to check string is palindrome
- Explanation of the C program to check string is palindrome with output.
- 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
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:
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.