Palindrome using string function
Palindrome String means that string spelled as same as in reverse. that is considering we have a string name now reverse that string if reversed string matches with the original string then that string is referred as palindrome string.
Example :
Ankit is not a palindrome
ABA is palindrome
In above example, Ankit => reverse string we get tikna as a string which is different from the original string so it’s not a palindrome. Similarly ABA => reverse string we get ABA which is same as an original string so its palindrome string.
program:
Palindrome program using string function
12345678910111213141516171819202122232425262728 | #include <stdio.h>#include <string.h>int main(){ char string1[20]; int i, length; int flag = 0; printf(“Enter a string:”); scanf(“%s”, string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; }} if (flag) { printf(“%s is not a palindrome”, string1); } else { printf(“%s is a palindrome”, string1); } return 0;} |
Output:

Explanation :
1. Start with writing the header files in program.
2. first write main function where program execution starts.
3. Declare variables
string1 => to store string
i, j => temporary variables
flag =0 => flag
length => to store length of string
4. Take a string input from user.
Example ; ABBA
5. Next calculate length of the string so that we can check for palindrome string
for this we use strlen() in string class.
length = strlen(string1);
6. After this loop length = 4
7. Next we check palindrome string from start to end
for(i=0;i < length ;i++) is to check string end
At iteration 1 :
i =0 , 0 < 4 this condition becomes true and loop executed
if(string1[i] != string1[length-i-1]) => A ! = A this condition become false and if block skipped and value of i is increment in for loop
At iteration 2 :
i =1 , 1 < 4 this condition becomes true and loop executed
if(string1[i] != string1[length-i-1]) => B ! = B this condition become false and if block skipped and value of i is increment in for loop
At iteration 3:
i =2 , 2< 4 this condition becomes true and loop executed
if(string1[i] != string1[length-i-1]) => B ! = B this condition become false and if block skipped and value of i is increment in for loop
At iteration 4 :
i =3 , 3 < 4 this condition becomes true and loop executed
if(string1[i] != string1[length-i-1]) => A ! = A this condition become false and if block skipped and value of i is increment in for loop
At iteration 5:
i =4, 40 < 4 this condition becomes false and loop execution stops
7. Now check for flag value if flag =1 then string is not palindrome.
8. And flag =0 then string is palindrome.
9. then print string is palindrome.
Recommended posts