0

Currently have a assignment from school to make a C program that checks if a user entered string is a palindrome or not. The program need to have 2 different functions (not counting the main function) and one is that checks if a string is a palindrome or not, and one that makes it so that the program recognize an uppercase letter as a lowercase letter. Example: the program recognize both "wow" and "Wow" as palindromes, as of now it only recognize the first one and not the second one. I have currently no clue how to proceed and help is much appriciated.

This is how my code currently looks:

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


int checkPalindrome(char* text) 
{
    int i, c = 0, l; 
    l = strlen(text); 
    for (i = 0; i < l / 2; i++)
    {
        if (text[i] == text[l - i - 1])
            c++; 

    }
    if (c == i)
        return 1; 
    else
        return 0; 
}

int setToLowercase()
{

}

int main()
{
    char text[1000]; 

    printf("Enter  the string: ");

    gets(text); 


    if (checkPalindrome(text)) 
        printf("string is palindrome"); 
    else
        printf("string is not palindrome"); 

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • One of the cool things about `char` in C is that if you cast it to an `int`, you get an ASCII representation of it. [(Here's an ASCII table reference.)](http://www.asciitable.com/) `char` 'A' becomes `int` 65, char `B` becomes `int` 66... `char` 'a' becomes `int` 97, `char` 'b' becomes `int` 98... and so on. If the ASCII number is between 65 and 90, it's a capital letter; you can convert the `char` to an `int`, add 32 to it, and then convert back to `char` to turn it into a lowercase letter. Maybe take your input, convert all letters to lowercase this way, and then use the palindrome solver? – Nick Reed Dec 05 '19 at 20:07
  • 2
    No cast needed Nick, char itself is an integer type. – foreverska Dec 05 '19 at 20:10
  • `if (toupper(text[i]) == toupper(text[l - i - 1]))` or use `tolower` it doesn't matter which. You don't need to change any string. And, you can simplify the function with `if (toupper(text[i]) != toupper(text[l - i - 1])) return 0;` – Weather Vane Dec 05 '19 at 20:11
  • Well, `'W' != 'w'` from your example, so the conditional in your `checkPalidrome` function is too strict. Seems like something the second function could fix. Something like `int compareCharNoCase(char c1, char c2)` maybe, where it returns one if `c1` and `c2` are both the same letter? – Unn Dec 05 '19 at 20:12

3 Answers3

2

If you're allowed, tolower() will convert a character to lowercase. Loop for string.

Otherwise take a gander at an ascii chart and figure the math/if case out.

foreverska
  • 585
  • 3
  • 20
1

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

The function gets is not a standard C function. It is unsafe. Instead use the standard C function fgets.

The function checkPalindrome does not change the passed string. So its parameter should be declared with the qualifier const.

int checkPalindrome(const char* text);

The return type of the function strlen is size_t. So the variable that accepts the value returned by the function also should have the type size_t.

To convert a character to lower case use the standard function tolower declared in the header <ctype.h>.

Below there is a demonstrative program that shows how the original program can be written.

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

char setToLowercase( char c )
{
    return tolower( ( unsigned char )c );
}

int checkPalindrome( const char *s ) 
{
    size_t n = strlen( s );

    size_t i = 0;

    while ( i < n / 2 && setToLowercase( s[i] ) == setToLowercase( s[n-i-1 ] ) )
    {
        i++;
    }

    return i == n / 2;
}

int main(void) 
{
    while ( 1 )
    {
        enum { N = 1000 };
        char s[N]; 

        printf( "Enter a string (Enter - exit): " );

        if ( fgets( s, N, stdin ) == NULL || s[0] == '\n' ) break;

        //  remove the appended new line character '\n'
        s[ strcspn( s, "\n" ) ] = '\0';

        printf( "The string is %spalindrome.\n",
                checkPalindrome( s ) ? "" : "not " );
        putchar ( '\n' );               
    }

    return 0;
}

The program output might look like

Enter a string (Enter - exit): AbCdEeDcBa
The string is palindrome.

Enter a string (Enter - exit): 

If you are compiling the program using the MS VS compiler then you can include the directive

#define _CRT_SECURE_NO_WARNINGS

Also the function setToLowercase could be inlined as for example

inline static char setToLowercase( char c )
{
    return tolower( ( unsigned char )c );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

I managed to solve the "problem" with the tolower() function. Thanks

void setToLowercase(char *string)
{
    int i;
    for (i = 0; string[i]; i++) {
        string[i] = tolower(string[i]);
    }
}