0

I'm trying to convert a string from upper case to lower case to check if it is a palindrome, however I keep getting the error:

"function declaration is not a prototype"

I already added #include <string.h> in the header, but it still doesn't work. How do I get around this issue?

This is the code:

int main (void)
{
    char *user_string, *user_string_rev;
    
    /* the malloc function is used to make sure that enough memory is allocated for the string and that it does not overwrite memory boxes of other variables. */
    user_string= (char*)malloc(BUFF_SIZE*sizeof(char));
    user_string_rev= (char*)malloc(BUFF_SIZE*sizeof(char));
    printf("Please enter a string:");
    fgets(user_string,BUFF_SIZE, stdin); /* fgets function take the string the user inputs and stores it into user_string. */
    user_string_rev=strcpy(user_string_rev, user_string); /*the strcpy takes the string the user inputs and copies it to user_string_rev. */
    strlwr(user_string_rev);
    
    palindrome_check(user_string,user_string_rev); /*this is the palindrome function used to check if the two strings are palindromes, it intakes two arguments, the two strings and does not return anything. */
    
    return 0;
    
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    strlwr is not a standard function. It's a Microsoft-only thing as far as I know. [see this question for a replacement](https://stackoverflow.com/questions/23618316/undefined-reference-to-strlwr) –  Jan 28 '18 at 19:01
  • It says to use the tolower function, however that does not work do you have any insight on howe to get around this problem? – matteociccozzi Jan 28 '18 at 19:14
  • when calling any of the heap allocation functions: (malloc, calloc, realloc) 1) always check (!=NULL) the returned value to assure the operation was successful. When the operation was not successful, use `perror()` to output the enclosed text AND the reason the system thinks the error occurred to `stderr`. 2) in C, the returned type is `void*`, which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc. – user3629249 Jan 28 '18 at 20:25
  • the expression: `sizeof(char)` is defined in the C standard as 1. Multiplying anything by 1 has no effect. Suggest removing that expression from the parameter to the function: `malloc()` – user3629249 Jan 28 '18 at 20:26
  • regarding: `fgets(user_string,BUFF_SIZE, stdin);` the function `fgets()` places the final '\n' (newline) in the input buffer. It is (usually) best to remove the newline. One way is: `char *newline = strchr( user_string, '\n' ); if( newline ) { *newline = '\0'; }` – user3629249 Jan 28 '18 at 20:30
  • regarding: `palindrome_check(user_string,user_string_rev);` the arrays are not 'reversed', the second parameter is just the same array with upper case letters replaced by the equivalent lower case letter. There is really no need to have other than the one parameter and for checking for a palandrome, the question fails to mention if upper case and lower case are to be treated as equivalent – user3629249 Jan 28 '18 at 20:53
  • palindrome_check is a function I wrote – matteociccozzi Jan 28 '18 at 21:40

2 Answers2

0

Replace :

strlwr(user_string_rev);

which is not a standard function with:

int i = 0;
while (user_string_rev[i])
{
    if (isalpha(user_string_rev[i]))
        user_string_rev[i] |= 32;
    ++i;
}

Don't forget to add the ctype header at the top of your .c file to use isalpha:

#include <ctype.h>
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
  • I ended up doing this and it worked: `*user_string_rev= tolower((unsigned char)*user_string_rev); *user_string=tolower((unsigned char)*user_string);` – matteociccozzi Jan 28 '18 at 19:18
  • 3
    The `|= 32` is not nice. Use `tolower()`, probably without the check since it doesn’t translate non-alpha characters. – Jonathan Leffler Jan 28 '18 at 20:13
  • @MatteoCiccozzi: consider adding that as an answer. – Jongware Jan 28 '18 at 20:33
  • @MatteoCiccozzi, The `tolower()` only works with a single character at a time, not the whole string. – user3629249 Jan 28 '18 at 21:45
  • well, it was already posted here https://stackoverflow.com/questions/16909302/how-to-set-a-string-to-all-lowercase. You don't need the check indeed since libc will do exactly the same. Both are equally fast. – Antonin GAVREL Jan 28 '18 at 23:55
0

the following proposed code:

  1. incorporates the comments to the question
  2. cleanly compiles
  3. properly checks for errors
  4. will treat a string that is nothing but a newline as NOT a palindrome

And now the proposed code:

#include <stdio.h>    // getline(), printf()
#include <stdlib.h>   // free()
#include <ctype.h>    // tolower()
#include <string.h>   // strlen(), strchr()

// prototypes
void palindrome( char *, size_t length );


int main( void )
{
    char   *inputStr = NULL;
    size_t  lengthStr = 0;

    printf("Please enter a string:");
    if( -1 != getline( &inputStr, &lengthStr, stdin ) )
    {
        size_t length  = strlen( inputStr );
        for( size_t i = 0; i < length; i++ )
        {
            inputStr[i] = (char)tolower( inputStr[i] );
        }


        char *newline = strchr( inputStr, '\n' );
        if( newline )
        {
            *newline = '\0';
            length--;
        }

        palindrome( inputStr, length );
    }

    free( inputStr );

    return 0;
}


void palindrome( char stringToCheck[], size_t length )
{
    size_t index = length - 1;  // don't check NUL terminator byte
    size_t i;

    for( i = 0; i < index; i++ )
    {
        if( stringToCheck[i] != stringToCheck[ index ] )
        {
            break;
        }

        index--;
    }

    if( i < index )
    {
        printf( "%s is not a palindrome\n", stringToCheck );
    }

    else
    {
        printf( "%s is a palindrome\n", stringToCheck );
    }
}
user3629249
  • 16,402
  • 1
  • 16
  • 17