Can someone please explain why this function will not compile.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

// This function determines if a string is a palindrome.
/* This function reverses the characters in the string
 * passed by the caller. */
void is_palindrome (char* string)
{
    int i;
    int length = strlen(string);
    int half_way = length/2;

    printf ("Function reverse called for string %s\n",
               string);

    for (i = 0; i < half_way; i++)
    {
        swap (&string[i], &string[length-1-i]);
    }
}

int main(void)
{
    char input[1000] = "";
    printf ("Enter a message: ");
    fgets(input, 1000, stdin);
    input[999] = 0;
 
    is_palindrome(input);

    return 0;
}

Where does that swap function come from. I'm betting that neither ctype.h, string.h, nor stdio.h define a function which matches the method header that you attempted to use in your call to swap().

Where does that swap function come from. I'm betting that neither ctype.h, string.h, nor stdio.h define a function which matches the method header that you attempted to use in your call to swap().

I thought the swap function was defined in #include <stdio.h>. Is that correct?

stdio.h defines functions used for input and output. Does swap() seem like such a function? And you can google as well as I can. Is swap defined in stdio.h?

Can someone please explain why this function will not compile.

It would be better if you explain why you think it doesn't compile. I assume the compiler says something to lead you to that conclusion. By posting what it says, we can tell you exactly why, and we won't have to guess.

>I thought the swap function was defined in #include <stdio.h>.
You guessed wrong[1]. Maybe instead of thinking from a position of ignorance, you should do some research and enlighten yourself.

[1] In some parallel universe where the standard C library contains a swap function, it would most likely be in <stdlib.h>.

[1] In some parallel universe where the standard C library contains a swap function, it would most likely be in <stdlib.h>.

you meant there's no swap function in standard C library right?(oh..my bad english...:-/) i didn't find anything in the link or google too..

input[999] = 0;

Also, why this is needed?

>you meant there's no swap function in standard C library right?
Yes.

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.