1

I have written a program trying to determine wether a string is a palindrome or not, regardless of special characters/uppcaser letters etc. I want to make it able to loop. I have used different methods for this, but it all seems to give me the same result.

When I press 1 at the end it basically takes whatever value I already gave it earlier and gives me the same response (wether or not it is palindrome and asks to restart again), as if its fast forwarding to the same question. I can then hit 0 and it closes like it should.

All the code is in there but Im pretty sure its something in main()

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

#define ARR_SIZE 50

int isPalindrom(char inputString[]);
void isAlphabet(char inputString[], char returnstring[]);
void smol(char inputString[]);

void smol(char inputString[])
{
    for (int i = 0; i < strlen(inputString); i++)
    {
        inputString[i] = tolower(inputString[i]);
    }
}


void isAlphabet(char inputString[], char returnstring[])
{
    int length, i, j = 0;
    length = strlen(inputString);

    for (i = 0; i < length; i++)
    {
        if (isalpha(inputString[i]) != 0)
        {
            returnstring[j] = inputString[i];
            j++;
        }
        returnstring[j] = '\0';
    }
}

int isPalindrom(char inputString[])
{
    inputString[strlen(inputString)] = '\0';
    int length = strlen(inputString);
    int length2 = length;

    char copy[ARR_SIZE];

    for (int i = 0; i < length2; i++)
    {
        copy[i] = inputString[length - 1];
        length--;
    }

    copy[length2] = '\0';

    int check = 0;

    for (int i = 0; i < length2; i++)
    {
        if (copy[i] == inputString[i])
        {
            check++;
        }
    }

    if (check == length2)
        return 1;
    else
        return 0;
}

int main(void)
{
    char string[ARR_SIZE];
    char returnstring[ARR_SIZE];
    int decide;
    int ret;

    do {
        printf("Enter a string!\n");

        fgets(string, ARR_SIZE, stdin);

        isAlphabet(string, returnstring);

        smol(returnstring);

        ret = isPalindrom(returnstring);

        if (ret == 1)
            printf("Palindrom!\n");
        else
            printf("Not a Palindrom\n");

        printf("Do you want to restart? Yes = 1, No = 0 ");
        scanf_s("%d", &decide);
    } while (decide == 1);

    return 0;
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 1
    With `isAlphabet(char inputString[], char returnstring[])`, after the call `returnstring[]` is not certainly a _string_ as it may lack a final _null character_. Consider `isAlphabet("", returnstring)` – chux - Reinstate Monica Nov 02 '21 at 19:52
  • Do not use `fgets()` and `scanf()`. Use `fgets()` only until you know why `scanf()` is bad. – chux - Reinstate Monica Nov 02 '21 at 19:56
  • notmenotyou, What is the goal of `inputString[strlen(inputString)] = '\0';`? Find the length of a string (where the _null character_ is) and then assigned that element to the _null character_? – chux - Reinstate Monica Nov 02 '21 at 19:57
  • @chux-ReinstateMonica I saw that too. I assume it's an attempt to remove the trailing newline from `fgets`, but it's already gone because `isAlphabet` removes it. `inputString[strlen(inputString) - 1] = '\0';`is a way to do it, but safer options exist: [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – Retired Ninja Nov 02 '21 at 20:12
  • 1
    @RetiredNinja I was hoping you linked [this](https://stackoverflow.com/a/27729970/2410359) ;-). – chux - Reinstate Monica Nov 02 '21 at 20:15

0 Answers0