In this tutorial, we will write a C program to read a string and check for palindrome without using string related function. Before that, you may go through the C topic below.
What is a Palindrome Number or String?
A number is said to be a Palindrome number if it remains the same when its digits are reversed or are the same as forward. It is also applied to the word, phrase or other sequences of symbols.
For example: 14141, 777, 272 are palindrome numbers as they remain the same even if reversed. If we take the example of ‘mam‘ or ‘madam‘, these are also palindrome words as we will get the same result even if we write it backward.
C Program to check a given string is palindrome without using the Built-in Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdio.h> int main() { char string[50], length = 0; int flag = 1, i; printf("Enter the String:\n"); gets(string); //\0 represent for string is ended for (i = 0; string[i] != '\0'; i++) { length++; } for (i = 0; i < length / 2; i++) { if (string[i] != string[length - 1 - i]) { flag = 0; break; } } if (flag == 1) printf("%s :- is a palindrome.", string); else printf("%s :- is not a palindrome.", string); return 0; } |
Output:
//First Run
Enter the String:
madam
madam :- is a palindrome.
//Second Run
Enter the String:
step on no pets
step on no pets :- is a palindrome.
//Thirsd Run
Enter the String:
how are you
how are you :- is not a palindrome.