저는 C 프로그램과 약간의 어려움을 겪고 있습니다! 문자열이 회문인지 여부를 확인해야합니다! 비 알파벳 문자에주의를 기울여서는 안되기 때문에 프로그램은이를 회문으로 인식해야합니다! "그는 악마로 살았습니까?" 그것은 내가 지금까지 가지고있는 작업은 다음과 같습니다C 문자열이 Palindrome인지 확인하는 프로그램
#include <stdio.h>
#include <stdlib.h>
int main()
{
char sentence[39];
int left = 0;
int right = 40;
printf("Enter a message: ");
fgets(sentence, 40, stdin);
while(1) {
while(left < right && !(isalpha(sentence[left])))
left++;
while(right > left && !(isalpha(sentence[right])))
right--;
if(left >= right)
break;
else {
if(sentence[left] != sentence[right]) {
printf("Not a Palindrome");
return 0;
}
left++;
right--;
}
}
printf("Palindrome");
return 0;
}
는 항상 인쇄하는 것 : NOT A 회문! 하나 인 경우에도.
처럼 보일 수 있습니다? 'fgets'이 문자열의 끝에'\ n'을 남기고 있다고 생각 했습니까? –
'right == 40'이 초기 값일 때'sentence [right]'는 유효하지 않습니다. – timrau
문자를 대문자 또는 소문자로 변환해야합니다. 'H! = h'. –