#C
C
Most of this code comes from an answer to Find the beginning of the first word that is a palindrom [sic] posted by BLUEPIXY, which outputs the word mom
. At lines 21 and 22, the hello
is printed by a code snippet at the end of an answer to printf(“%%%s”,“hello”) by user142019 (who fortunately added a semicolon at the end), and the space between the words comes from an answer to How to print space with padding in c by M Oehm.
###Output:
Output:
%hello mom
I wasn't able to eliminate the extra spaces or the percent character, unfortunately :(
###Source code:
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isPalindrome(const char *str){
const char *front, *back;
front=str;
back =str + strlen(str)-1;
for(;front<back;++front,--back){
while(!isalpha(*front))++front;//isalnum?
while(!isalpha(*back))--back;
if(front > back || tolower(*front)!=tolower(*back))
return 0;
}
return 1;
}
int main(){
const char *data="Hello mom and dad, how is it going?";
char *p, *src;
printf("%%%s", "hello");
printf(" ");
p=src=strdup(data);
for(;NULL!=(p=strtok(p, " \t\n,.!?"));p=NULL){
if(isPalindrome(p)){
printf("%s\n", p);
break;
}
}
free(src);
return 0;
}