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;
}