0

I try to cod C program where the user can input a string and the program should check if the string is Palindrome or not. The string can also be a sentence such as "no lemon, no melon". I have a function "checkForSpaceAndChar" that removes spaces from the sentence and another function "isPalindrome" that checks if the string is Palindrome. Now I try to figure out how I can first take the entered string and remove the space and special characters and then check if the string is a palindrome.

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

int isPalindrome(char inputeString[]){
    int begin = 0, end = strlen(inputeString) - 1;

    while (end > 1) {
        if (inputeString[begin++] != inputeString[end--]) {
            return 0;
        }
        else {
            return 1;
        }
    }
}

char checkForSpaceAndChar(char stringWithoutSpace[], char newArray[]) {

    for (int i = 0; i < strlen(stringWithoutSpace); i++) {
        if (isalpha(stringWithoutSpace[i]) != 0) {
            stringWithoutSpace[i] = newArray[i];
        }
    }
}
#define SIZE 1000

int main(void) {
    int repeat = 1;
    char arrayPalindrome[SIZE], newArray[SIZE];

    while (repeat == 1) {
        printf("Enter a sentence: ");
        scanf("%s", arrayPalindrome);

        checkForSpaceAndChar(arrayPalindrome, newArray);

        if (isPalindrome(arrayPalindrome) == 0) {
            printf("This sentence is not a palindrome.");
        }
        if (isPalindrome(arrayPalindrome) == 1) {
            printf("This sentence is a palindrome.");
        }
        printf("\n\nDo you want to enter another sentence (0 for no, 1 for yes)?");
        scanf_s("%d", &repeat);
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
LeoParden
  • 17
  • 2
  • 1
    You have described what you want to do but you have not described what specific error or problem you have with the code as shown. – kaylum Dec 02 '20 at 23:20
  • 2
    `isPalindrome()` is not correct. It always returns during the first iteration of the loop, so it just checks whether the first character is the same as the last character. `return 1` should be after the loop, not in `else`. – Barmar Dec 02 '20 at 23:23
  • OT: It is inefficient to call `isPalidrome` twice. Just call it once and save the return value. Though in this case it's more common to have an `if () { ..} else { .. }` rather than checking the return value twice. – kaylum Dec 02 '20 at 23:23
  • 1
    `scanf("%s")` has all the same issue as `gets`. Never use `gets`. https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used As a direct corollary, never use `scanf("%s"...)` – William Pursell Dec 02 '20 at 23:30
  • regarding: `for (int i = 0; i < strlen(stringWithoutSpace); i++) {` the function: `strlen()` returns a `size_t` which is a unsigned value. This statement is comparing it to a `int` which is a signed value. – user3629249 Dec 03 '20 at 21:19
  • two of the posted functions have an execution path that does not end in a `return value` statement, however, the function signatures say the returned type is not `void` – user3629249 Dec 03 '20 at 21:21
  • OT: a palindrome can be checked by only comparing 1/2 of the characters. When the comparison reaches the middle of the char array, there is no need to continue comparing – user3629249 Dec 03 '20 at 21:23

2 Answers2

1

checkForSpaceAndChar() needs to use two index variables, one for the input array and another for the output array. The way you've written it, it copies all the alpha characters to newArray, but leave the elements corresponding to the non-alpha characters unchanged. So you get gaps of uninitialized entries.

And it should be copying in the other direction, from stringWithoutSpace to newArray.

Since checkForSpaceAndChar doesn't return anything, it should be declared void.

void checkForSpaceAndChar(char stringWithoutSpace[], char newArray[]) {
    int j = 0;
    for (int i = 0; i < strlen(stringWithoutSpace); i++) {
        if (isalpha(stringWithoutSpace[i]) != 0) {
            newArray[j++] = stringWithoutSpace[i];
        }
    }
}

Since newArray is where it copies to, you need to use that as the argument to isPalindrome().

        if (isPalindrome(newArray)) {
            printf("This sentence is a palindrome.");
        } else {
            printf("This sentence is not a palindrome.");
        }
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

To check whether a sequence of alpha characters of a string form a palindrome there is no need to remove non-alpha characters. Moreover if the user will pass a string literal as a function argument then such a function that removes non-alpha characters will invoke undefined behavior..

So such an approach is not good.

The function isPalindrome can be written for example the following way without the requirement to remove non-alpha characters.

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


int isPalindrome( const char *s )
{
    const char *p = s + strlen( s );

    do
    {
        while ( s != p && !isalpha( ( unsigned char )*s ) ) ++s;
        
        while ( s != p && !isalpha( ( unsigned char )*--p ) );
    } while ( s != p && *s == *p && ++s );
    
    return s == p;
}

int main(void) 
{
    const char *s = "a bc, ba!";
    printf( "\"%s\" is palindrome: %s.\n", s, isPalindrome( s ) ? "true" : "false" );

    return 0;
}

The program output is

"a bc, ba!" is palindrome: true.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335