Hello everyone,
I had been working on this project recent days and finally got it work. The problem is I couldn't do it the way question asked me so, I did it my way. The problem is regarding Palindrome. I check if the word or sentence is a palindrome and the shows you the output. You can check the code to see how it works.
My question is how can I modify it so that it will have 2 functions. 1 non-recursive function to determine if a string is palindrome or not. Add another function (recursive) doing the same thing. I tried placing them in between my program but it never worked. Can someone help me with that?
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STR_LEN 80
typedef enum { false = 0, true } bool;
void erase(char *, size_t);
bool isPalindrome(const char *, const char *);
char in[MAX_STR_LEN];
int main(int argc, char *argv[]) {
size_t x;
while (1) {
printf("> ");
fgets(in,MAX_STR_LEN,stdin);
/*
* Remove trailing newline from entered string, then
* remove all spaces and punctuation from string, and
* convert all to lower case.
*/
*(strchr(in,'\n')) = '\0';
for (x = 0; x < strlen(in); ) {
in[x] = tolower((int)in[x]);
if (isspace((int)in[x]) || ispunct((int)in[x])) {
erase(in, x);
} else {
x++;
}
}
/*
* Check if the result is a palindrome.
*/
printf("\"%s\" is ",in);
if (isPalindrome(in, in + strlen(in) - 1) == false) {
printf("not ");
}
printf("a palindrome.\n\n");
}
return 0;
}
bool isPalindrome(const char *p, const char *q) {
if (q < p) return true;
if (*q != *p) return false;
return isPalindrome(p + 1, q - 1);
}
void erase(char *s, size_t x) {
memmove(&s[x], &s[x+1], strlen(s) - x);
}
Thanks