#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 3 /* >= 2 */
char *getline(void)
{
static char inbuff[BUFFSIZE];
char *outbuff_malloc, *tmpbuff;
char *p;
int fEOL;
if ((outbuff_malloc = malloc(1)) == NULL)
return NULL;
*outbuff_malloc = '\0';
fEOL = 0;
do {
if (fgets(inbuff, BUFFSIZE, stdin) == NULL)
break;
for (p = inbuff; *p != '\0'; p++)
;
if (*(p - 1) == '\n') {
*(p - 1) = '\0';
fEOL = 1;
}
if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) ==NULL) {
free(outbuff_malloc);
return NULL;
}
strcat(tmpbuff, inbuff);
outbuff_malloc = tmpbuff;
} while (!fEOL);
return outbuff_malloc;
}
int main() {
int f;
char *s, *p, *q;
printf("文字列を入力してください.: ");
s = getline();
f = 1;
for (p = s, q = s + strlen(s) - 1; p < q; p++, q--) {
printf("%c:%c\n", *p, *q);
if (*p != *q) {
f = 0;
break;
}
}
if (f == 1)
printf("回文です.\n");
else
printf("回文ではありません.\n");
free(s);
return 0;
}
/* end */