#include <stdio.h>
#include <stdlib.h>


int main()
{
    char word[50];
    int wordLength, i, reverseCount;

    reverseCount = 0;

    system( " color F5 " );

    printf( "\n How many letters does the word have? : " );
    scanf( "%i", &wordLength );

    printf( " \n Enter word, letter by letter " );

    for( i = 0; i <= wordLength; i++ );
    {
        scanf( "%c", &word[i] );
    }

    while( reverseCount != 0 )
    {
        for( reverseCount = i - 1; reverseCount != 0; reverseCount-- && i++ )
        {
            i = 0;
            if( word[i] != word[reverseCount] )
            {
                printf( " \n Word is not a palindrome. " );
            }
        }
    }

    system( " pause " );

    return 0;
}

Help? It just without ends anything being inputted.

Recommended Answers

All 2 Replies

Line 21, add a space before the %c: scanf(" %c",&wordlength). That is needed to "eat" the newline that was left over in the input buffer, from when you hit enter.

Note that a palindromes don't take punctuations and word dividers into account. For example:

Bush saw Sununu swash sub.

is a palindrome, although your application would say it's not. These types of problems are easy to think about recursively. The general idea would be that 'the first and last character should match' and the remains of the string should be a palindrome as well. You would end up with something like this:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>

bool IsPalindrome (const char *string, const int size)
{
    assert (size >= 0);

    // A word consisting of one letter or less is palindrome.
    if (size <= 1)
    {
        return true;
    }
    // non-letters may be skipped.
    else if (!isalpha(string[0]))
    {
        return IsPalindrome(string + 1, size - 1);
    }
    // the same goes for the end
    else if (!isalpha(string[size - 1]))
    {
        return IsPalindrome(string, size - 1);
    }
    // The first and final character are both letters. These must be equal.
    else
    {
        return (tolower(string[0]) == tolower(string[size - 1])) && IsPalindrome(string + 1, size - 2);
    }
}

int main(void)
{
    const char* tests[] = {
        "Bush saw Sununu swash sub.",
        "Do, O God, no evil deed! Live on! Do good!",
        "This is a random text",
        "Flee to me, remote elf.",
        "No, I told Ed \"lotion.\"",
        "Never gonna give you up, never gonna let you down, Never gonna run around and desert you."
    };

    const int testsCount = sizeof(tests) / sizeof(char*);
    int i = 0;

    for (i = 0; i < testsCount; i++)
    {
        if (IsPalindrome(tests[i], strlen(tests[i])))
        {
            printf("\"%s\" is a palindrome!\n", tests[i]);
        }
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.