0

A string consisting of words, no longer than 100 characters, is supplied. Words consist of Latin characters and are separated by a single space. It is necessary to output to the standard output stream a string containing only the words palindromes. The source data must be read into memory as a whole and all manipulations must be carried out in memory, the result obtained in memory and then printed.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int check(char str[])
{
    int i, length;

    length = strlen(str);

    for (i = 0; i < length; i++)
        if (str[i] != str[(length - 1) - i]) return 0;
    return 1;
}

int main(void)
{
    char str[100];
    char* t;

    gets(str);
    t = strtok(str, " ");


    while (t != NULL) {
        if (check(t) == 1) {
            printf("%s ", t);

        }

        t = strtok(NULL, " ");
    }
    _getch();
    return 0;
}

this is my code (it fails the tests) Please help me fix the code

Dominique
  • 16,450
  • 15
  • 56
  • 112
Notoriuss
  • 1
  • 1
  • 1
    Never *ever* use `gets`! It's [a dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that have even been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Nov 26 '18 at 13:38
  • As for your problem, this might be a good time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Nov 26 '18 at 13:40
  • Lastly, please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then edit your question to improve it. – Some programmer dude Nov 26 '18 at 13:44
  • I don't know what else you can change (thanks for the fgets I will definitely change it), who can email me the corrected code please – Notoriuss Nov 26 '18 at 13:54
  • Why `for (i = 0; i < length`? `length/2` seems enough. – Paul Ogilvie Nov 26 '18 at 14:01
  • If strings are "no longer than 100 characters", then why is `str` only 80?? – Paul Ogilvie Nov 26 '18 at 14:03
  • (I am recently learning C and do not know much) How to complete this part of the task? The source data should be read in memory as a whole, and all manipulations should be performed in memory, the result obtained in memory and then printed. – Notoriuss Nov 26 '18 at 14:19

1 Answers1

0
  1. Instead of fgets use function getline(&buffer, &size, stdion) for example.
  2. Your for loop in the check function works fine but resolve another problem, than that you expected.
  3. Palindrome a word or group of words that is the same when you read it forwards from the beginning or backwards from the end.
alexzok
  • 81
  • 7
  • this code it works, try to enter any words of palindromes and it will output them if this is true (but for some reason my tests do not pass this code 0/16) – Notoriuss Nov 26 '18 at 16:24