This is for homework at school. I'm not asking for correct answers just a push in the right direction. An explanation as to why this is going wrong would be great as well as an explanation of the correct method. What this C program should be doing is reading in the user input without spaces and punctuation and assigning it to the character array string. This array should then be passed into the function palindrome. Palindrome should length of the string and if equal to either 1 or 0 return TRUE or 1 else move on and check the first and last character of the string. If they match retrieve the 2nd and 2nd to last characters, and all the characters in between, and pass that into the function palindrome.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
typedef int Bool;
Bool palindrome(char str[]);
main()
{
char string[1000], ch;
int i = 0;
printf("Enter a message: ");
while((ch = getchar()) != '\n'){
if(isspace(ch)==FALSE || ispunct(ch)==FALSE);
string[i] = tolower(ch);
i++;
}
string[i] = '\0';
printf("\n");
if(palindrome(string))
printf("Palindrome\n");
else
printf("Not a palindrome\n);
return 0;
}
Bool palindrome(char str[])
{
int length = strlen(str);
if((length == 1) || (length == 0))
return TRUE;
else
{
if((str[0] == str[length - 1])
{
char str_new[length-1];
int i, j;
for(i = 1, j = 0; str[i]!=str[length-1]; i++, j++)
str_new[j] = str[i];
str_new[i] = '\0';
palindrome(str_new);
}
else
return FALSE;
}
}
Not matter what the input it always prints that the string given was a not a palindrome. For instance when I input
He lived as a devil, eh?
it prints out
Not a palindrome
Also when I edited the program to check what was in the array string using the previous input it was
He lived as a devil, eh?
Feel free to comment on any other aspects of my code that you see code use improvement. They really don't provide anything other than a yes its right or a no it isn't concerning our code.
EDIT:
I did check to see what the value in the char array, string, was. I say this before the last blockquote.