/*
* C Program to Check whether a given String is Palindrome or not
* using Recursion
*/
#include <stdio.h>
#include <string.h>
void check(char[], int);
int main() {
char string[25];
printf("enter a string:\n");
scanf("%s", string);
check(string, 0);
return 0;
}
void check(char word[], int index) {
int len = strlen(word) - (index + 1);
if (word[index] == word[len]) {
if (index + 1 == len || index == len) {
printf("The entered word is a palindrome.\n");
return;
}
check(word, index + 1);
}
else {
printf("The entered word is not a palindrome.");
}
}
/*
* C program to read a string and check if it's a palindrome, without
* using library functions. Display the result.
*/
#include <stdio.h>
#include <string.h>
int main() {
char string[25],
reverse_string[25] = { '\0' };
int flag = 0,
i,
length = 0;
fflush(stdin);
printf("enter string...\n");
gets_s(string);
for (i = 0; string[i] != '\0'; i++) {
length++;
}
//hello = 6 | start form 5 |
//olleh
//reverse the string
for (i = length - 1; i >= 0; i--) {
reverse_string[length - i - 1] = string[i];
}
for (i = 0; i < length; i++) {
if (reverse_string[i] == string[i]) {
flag = 1;
}
else {
flag = 0;
}
}
if (flag == 1) {
printf("%s is a palindrome\n", string);
}
else {
printf("%s is not a palindrome\n", string);
}
return 0;
}
/*
* C Program to Check whether a given String is Palindrome or not
* using Recursion
*/
#include <stdio.h>
#include <string.h>
void check(char[], int);
int main() {
char string[25];
printf("enter a string:\n");
scanf("%s", string);
check(string, 0);
return 0;
}
void check(char word[], int index) {
int len = strlen(word) - (index + 1);
if (word[index] == word[len]) {
if (index + 1 == len || index == len) {
printf("The entered word is a palindrome.\n");
return;
}
check(word, index + 1);
}
else {
printf("The entered word is not a palindrome.");
}
}