C Tutorial
This c program uses the while loop and character array and finds the whether input string is a polindrome or not.
1) Finds the length of the input string. 2) Getting reverse string using string length. 3) Compares reverse string with input string, input sting is a polindrome if both same, otherwise not.
#include<stdio.h> int main() { char str[20], reverse_str[25]; int i, len = 0; printf("Enter a string: "); gets(str); //Gets the string length while (str[len] != '\0') { len++; } //reverse string i = 0; while (i < len) { reverse_str[i] = str[len-i-1]; i++; } reverse_str[i] = '\0'; //Compares and checks whether input string is polindrome or not. i = 0; while (i < len) { if (reverse_str[i] != str[i]) { printf("%s is not a palindrome.\n", str); return 0; } i++; } printf("%s is a palindrome.\n", str); return 1; }Output:
$ cc string-polindrome.c string-polindrome.c: In function ‘main’: string-polindrome.c:9:5: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] gets(str); ^~~~ /tmp/ccs2K9ca.o: In function `main': string-polindrome.c:(.text+0x2b): warning: the `gets' function is dangerous and should not be used. $ ./a.out Enter a string: malayalam malayalam is a palindrome. $ ./a.out Enter a string: testing testing is not a palindrome.
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page