Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- #include <ctype.h>
- #include <string.h>
- int is_it_palindrome(const char* str)
- {
- int lenght;
- int has_alnum = 0;
- lenght = strlen(str);
- const char* start = str + 0;
- const char* end = str + lenght - 1;
- while (start <= end)
- {
- if (!isalnum(*start))
- {
- start++;
- }
- else if (!isalnum(*end))
- {
- end--;
- }
- else if (toupper(*start) == toupper(*end))
- {
- has_alnum = 1;
- start++;
- end--;
- }
- else
- {
- return 0;
- }
- }
- return has_alnum;
- }
- int longest_palindrome(char *word) {
- int n = strlen(word);
- int ehpalindromo = is_it_palindrome(word);
- if (ehpalindromo == 1)
- {
- return n;
- }
- int i, j, longest_palindrome_length = 0, longest_palindrome_index = 0;
- for (i = 0; word[i] != '\0'; i++) {
- for (j = i + 1; word[j] != '\0'; j++) {
- int is_palindrome = 1;
- for (int k = i; k < j; k++) {
- if (word[k] != word[j - k + i - 1]) {
- is_palindrome = 0;
- break;
- }
- }
- if (is_palindrome && j - i > longest_palindrome_length) {
- longest_palindrome_length = j - i;
- longest_palindrome_index = i;
- }
- }
- }
- return longest_palindrome_length;
- }
- int main() {
- char word[100];
- int longest_palindrome_length;
- printf("Enter a word: ");
- scanf("%s", word);
- longest_palindrome_length = longest_palindrome(word);
- printf("The longest palindrome in the word '%s' is of length %d.\n", word, longest_palindrome_length);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement