-1

I wanted to do a palindrome but when I do the code below:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char string[100];
    int comparison;
    char again;
    printf("This program will determine whether a word is a Palindrome or not.\n");
    do
    {
start:
        int c;
        printf("Enter the word: \n");
        scanf("%s", string);

        int length = strlen(string);
        char palindrome[length];
        for(int i = 0; i<length; i++)
        {
            palindrome[i] = string[length-1-i];
            printf("%c\n", palindrome[i]);
            int validation = isalpha(palindrome[i]);
            if(validation==0)
            {
                printf("Invalid Input! The input must be letters.\n");
                goto jump;
            }
        }
        printf("%s\n", palindrome);
        comparison = strcmp(string,palindrome);
        if(comparison == 0 )
        {
            printf("The word %s is a palindrome.\n", palindrome);
        }
        else
        {
            printf("The word %s is not a palindrome.\n", palindrome);

        }
        printf("Do you want to restart the code? Input Y to restart, otherwise any key to terminate  \n" );
        scanf("%s", &again);
        while ( (c = getchar()) != '\n' && c != EOF ) { }
    }
    while((again == 'Y') || (again == 'y'));//this will then loop back the code if again is Y, otherwise continues to the next chunk
    printf("Code terminated");
    return 0;


jump://If an invalid input will be placed, the code will jump here
    printf("Do you want to restart the program? Input Y to restart, otherwise any key to terminate  \n" );
    char again2;
    scanf(" %c", &again2);

    if((again2 == 'Y') || (again2 == 'y'))
    {
        goto start;//jumpts to the start on the top
    }
    printf("Code terminated");
    return 0;
}

When I input racecar, the code will execute correctly.

But when I input the word civic I get this:

civic c i v i c civic⌂

The word civic⌂ is not a palindrome.

Why is there an additional character ⌂?Thank you

Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14
  • `char palindrome[length];` --> Hint: where is the space for the _null character_ and where does it get assigned? – chux - Reinstate Monica Jan 10 '22 at 05:05
  • 1
    Side note: Your code would be a lot easier to read for yourself and other people if you used consistent [indentation](https://en.wikipedia.org/wiki/Indentation_style). Meanwhile, an edit proposal has been submitted, which will fix this (once it has been accepted). – Andreas Wenzel Jan 10 '22 at 07:15
  • sidenote: whenever you feel a need to use `goto` , think about the structure of your program, you are then probably doing something wrong. e.g. use functions instead of having all code in main. – AndersK Jan 10 '22 at 07:37
  • Always check the return code of `scanf`. – DevSolar Jan 10 '22 at 07:38
  • May want to check some of the other [Palindrome Questions](https://stackoverflow.com/a/55414017/3422102) which may provide further learning in how to approach the problem. – David C. Rankin Jan 10 '22 at 08:01

1 Answers1

2

Add null terminator ('\0') at the end of the string palindrome.

For example:

palindrome[length] = '\0';

Add the above line after copying all the character from string array to palindrome array.

If your string is not terminated with \0, it might still print the expected output because following your string is a non-printable character in your memory. This is a bug though, since it might blow up when you might not expect it. Always terminate a string with '\0'.

Note: As you're adding length number of character in the palindrome array, declare the size of palindrome array length +1 ( i.e. char palindrome[length + 1];)

Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14