How to print the smallest palindrome words in a string with C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>

void PrintLongestPalindromeWord(char str[], char* delimiter) {
    char* word = strtok(str, delimiter);
    size_t min = INT_MAX;
    char minword[16];

    while (word != NULL) {
        char* p = _strdup(word);
        int result = _stricmp(word, _strrev(p));
        free(p);
        if (result == 0 && strlen(word) >= 3) {
            size_t len = strlen(word);
            if (len < min) {
                min = len;
                strcpy(minword, word);
            }
        }
        word = strtok(NULL, delimiter);
    }

    puts(minword);
}

int main(void)
{
    char s[] = "c madam c++ civic java rotator pytyp dart php";

    PrintLongestPalindromeWord(s, " ");

    return 0;
}




/*
run:

php

*/

 



answered Dec 28, 2023 by avibootz

Related questions

2 answers 147 views
1 answer 158 views
3 answers 186 views
2 answers 156 views
1 answer 117 views
2 answers 176 views
2 answers 128 views
...