이것은 내가 작성한 palindrome 검사기입니다. 마지막 항목이 구두점 일 때 구두점이 있든 없든 모든 입력에 대해 작동합니다. 이 경우에는 그것을 건너 뛰지 않고 비교하고 실제로 그것이있을 때 회문이 아니라고 말합니다. EX (살았던 악마는 회문이 아니라 살았고, 악마가 될 것입니다).포인터를 사용하여 끝에 만 구두점을 비교하는 Palindrome
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#define max 180
bool is_palindrome(const char *message);
int main()
{
char message[max+1];
printf("Enter a message: ");
gets(message);
if(!*message)
{
printf("input error");
return 0;
}
if (is_palindrome(message)) printf("Palindrome\n");
else printf("Not a palindrome");
return 0;
}
bool is_palindrome(const char *message)
{
char *p, *p2;
bool palindrome = true;
p = message;
p2 = message;
for(;;)
{
while(*p)p++;
while(*p2)
{
while(!isalpha(*p)) p--;
while(!isalpha(*p2)) p2++;
if (toupper(*p) != toupper(*p2))
{
palindrome = false;
break;
}else
{
p--;
p2++;
}
}
break;
}
return palindrome;
}
'gets '를 사용하지 마십시오. 우리 한테 그렇게하지 마라. 그것은 위험한 기능이므로 C 표준에서 제거되었습니다. 아마도 ['fgets'] (http://en.cppreference.com/w/c/io/fgets)를 대신 사용하십시오. –
나는 K.N.의 현대 접근 교과서를 C 프로그래밍에서 배웠다. 그래서 나는 전체 라인을 읽는 데 사용되는 것처럼 그 기능을 선택했습니다. 그러나 미래 참고를 위해 그것을 알고있는 것이 좋다 당신을 감사하십시오. – OliviaA
[gets 함수가 너무 위험해서 사용해서는 안되는 이유는 무엇입니까?] (https://stackoverflow.com/q/1694036/995714) –