On this article, we will be able to write a C program to test if a string is a palindrome. A string is claimed to be palindrome if it stays the similar on studying from each ends. It signifies that whilst you opposite a given string, it must be the similar as the unique string. As an example, the string ‘degree’ is a palindrome as it stays the similar whilst you learn it from the starting to the top and vice versa. On the other hand, the string ‘Simplilearn’ isn’t a palindrome because the opposite of the string is ‘nraelilpmis,’ which isn’t the identical.
Algorithmic Good judgment of C Program for String Palindrome
To put in writing a C program for string palindrome, we want to practice the good judgment beneath:
- Create a serve as to test if the string is a palindrome: isPalindrome(str)
- Initialize indexes for high and low ranges to be 0 and n-1, respectively
- Till the low index (l) is not up to the prime index (h), do the next:
- If str(l) isn’t like str(h), go back false
- If str(l) and str(h) are identical, increment l, i.e., l++ and decrement h, i.e., h–
- If we achieve this step, it approach there is not any mismatch, and the string is a palindrome; in a different way, if step 3.A is correct, it’s not a palindrome.
Let’s use this good judgment to jot down a C program to test if the given string is a palindrome.
#come with <stdio.h>
#come with <string.h>
// Imposing the good judgment in a serve as
void isPalindrome(char str[]){
// Initializing indexes
int l = 0;
int h = strlen(str) – 1;
// Step 4 to stay checking the characters till they’re identical
whilst (h > l){
if (str[l++] != str[h–]){
printf(“%s isn’t a palindrome stringn”, str);
go back;
}
}
printf(“%s is a palindrome stringn”, str);
}
// Driving force program
int major(){
isPalindrome(“degree”);
isPalindrome(“radar”);
isPalindrome(“Simplilearn”);
go back 0;
}
Output:
As you’ll see within the above output, the strings ‘degree’ and ‘radar’ that stay the similar after reversing are displayed as a palindrome. On the other hand, for this situation, we gave the strings ourselves. Within the coming examples, we will be able to ask the consumer to go into the string and write a C program to test a string palindrome the usage of quite a lot of strategies.
C Program for String Palindrome The use of the Same old Means
We will be able to write a C program for string palindrome the usage of an ordinary way with none serve as or libraries. On this instance, we will be able to:
- Learn the string entered by way of the consumer the usage of will get(s)
- Calculate the period and retailer it in a variable
- Initialize low and high indexes
- Evaluate the characters at high and low index
- Use for loop to stay evaluating till a mismatch is located
Right here’s the implementation of checking a string palindrome the usage of an ordinary way.
#come with <stdio.h>
#come with <string.h>
int major(){
char str[1000];
int l,n,comp=0;
printf(“Input the string to test : “);
will get(str);
n=strlen(str);
for(l=0;l<n/2;l++){
if(str[l]==str[n-l-1])
comp++;
}
if(comp==l)
printf(“The entered string is a palindrome”);
else
printf(“The entered string isn’t a palindrome”);
go back 0;
}
Output:
Output:
C Program for String Palindrome The use of a Serve as
The good judgment for writing a C program for string palindrome the usage of a serve as is sort of the similar as that whilst the usage of an ordinary way. The numerous distinction is this time we will be able to create a serve as for checking palindrome. Additionally, after studying the string the usage of will get(s), we will be able to go it as an issue to the serve as.
Right here’s the C program to test if a string is a palindrome the usage of a serve as.
#come with <stdio.h>
#come with <string.h>
// Growing the serve as
int isPalindrome(char *str){
int l,comp=0,n;
n=strlen(str);
for(l=0;l<n/2;l++){
if(str[l]==str[n-l-1])
comp++;
}
if(comp==l)
go back 1;
else
go back 0;
}
int major(){
char str[1000];
printf(“Input the string to test: “);
will get(str);
if(isPalindrome(str))
printf(“The entered string is a palindrome”);
else
printf(“The entered string isn’t a palindrome”);
go back 0;
}
Output:
Output:
C Program for String Palindrome The use of Recursion
The set of rules for writing a C program for string palindrome is identical with just a unmarried main distinction: the serve as will stay calling itself recursively till the decrease index isn’t not up to part the period of the string, i.e., l<n/2. For the reason that serve as will stay calling itself recursively, we will be able to now not have to make use of the for loop this time.
Instance
#come with <stdio.h>
#come with <string.h>
void take a look at(char [], int);
int major(){
char str[15];
printf(“Input a phrase to test: “);
scanf(“%s”, str);
isPalindrome(str, 0);
go back 0;
}
void isPalindrome(char str[], int l){
int n = strlen(str) – (l + 1);
if (str[l] == str[n]){
if (l + 1 == n || l == n){
printf(“The entered phrase is a palindromen”);
go back;
}
isPalindrome(str, l + 1);
}
else
{
printf(“The entered phrase isn’t a palindromen”);
}
}
Output:
Output:
C Program for String Palindrome The use of String Library Purposes
For this situation, we will be able to be the usage of 3 string library purposes outlined within the string.h header document: strcpy, strrev, and strcmp. Those 3 purposes will permit us to replicate, opposite, and evaluate strings, the 3 necessities for checking a palindrome string.
Right here’s the C program for string palindrome the usage of string library purposes.
#come with <stdio.h>
#come with <string.h>
int major(){
char inArray[100], revArray[100];
printf(“Input the string to test: “);
scanf(“%s”, inArray);
// Copying enter string
strcpy(revArray, inArray);
// Reversing the string
strrev(revArray);
// Evaluating the reversed string with enter string
if(strcmp(inputArray, reversedArray) == 0 )
printf(“%s is a palindrome.n”, inputArray);
else
printf(“%s isn’t a palindrome.n”, inputArray);
getch();
go back 0;
}
Output:
Be aware: The above program makes use of the strrev() serve as, which is to be had simplest in ANSI C (Turbo C/C++ compilers). Therefore, if you’re the usage of an ordinary GCC compiler, the above code would possibly throw an error.
Conclusion
On this article, you’ve got realized easy methods to write a C program for string palindrome in several tactics. You’ll be able to additionally write it the usage of guidelines in C. You’ll be able to additionally take a look at writing a program in C++ to test a string palindrome. C++ is a longer model of C programming. In case you are new to C++, you’ll check with Simplilearn’s C++ Educational for Learners to get a transparent figuring out of the entire basics. But even so that, you’ll additionally join our SkillUp platform. The platform provides a large number of loose lessons in several programming languages to reinforce your talents in any programming language, together with C and C++.
You’ll be able to additionally opt for our Complete Stack Developer – MERN Stack, a certification route that gives coaching in virtually 30 programming languages and gear. To position it merely, it help you get the mastery in more than one building talents required to grow to be a full-stack developer and land a possibility to paintings for the most important firms within the device building international.
supply: www.simplilearn.com