How to print the palindrome words in a string with C

3 Answers

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

void PrintPalindromeWords(char str[], char* delimiter) {
    char* word = strtok(str, delimiter);

    while (word != NULL) {
        int result = _stricmp(word, _strrev(_strdup(word)));
        if (result == 0 && strlen(word) >= 3)
            puts(word);
        word = strtok(NULL, delimiter);
    }
}

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

    PrintPalindromeWords(s, " ");

    return 0;
}





/*
run:

madam
civic
pytyp

*/

 



answered Nov 12, 2022 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void PrintPalindromeWords(char str[], char* delimiter) {
    char* word = strtok(str, delimiter);

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

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

    PrintPalindromeWords(s, " ");

    return 0;
}





/*
run:

madam
civic
pytyp

*/

 



answered Nov 12, 2022 by avibootz
edited Dec 28, 2023 by avibootz
0 votes
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char string[] = "c madam c++ civic java pytyp dart";
    char delimiters[] = " ";

    char* p = strtok(string, delimiters);

    while (p) {
        char* word = _strdup(p);
        if (strcmp(word, _strrev(p)) == 0 && strlen(p) >= 3) {
            puts(p);
        }
        free(word);

        p = strtok(NULL, delimiters);
    }


    return 0;
}




/*
run:

madam
civic
pytyp

*/

 



answered Dec 28, 2023 by avibootz

Related questions

1 answer 144 views
1 answer 157 views
2 answers 147 views
2 answers 156 views
1 answer 116 views
2 answers 176 views
1 answer 102 views
...