첫 번째 if 문에 문제가 있습니다. 프로그램이 루프에 들어가면 if 문을 건너 뜁니다. 내 프로그램은 입력이 palindrome인지 테스트 한 다음 인쇄합니다 ('Reverse'배열은 테스트를 통과하면 원래 구문을 뒤로 인쇄합니다). 회문문이 "madam I am adam"인 경우 출력은 "madamimadam"이어야하며 대문자 또는 구두점이 없어야합니다. 배열에서 처리 할 것인지, 아니면 그 문자를 꺼내기위한 다른 조건부 테스트가 필요한지 확실하지 않습니다. 누군가가 내 코드를 살펴보고 무엇이 잘못되었는지/내가 할 수있는 것을 말해 주면 크게 감사하겠습니다. 문 test = 1
이 test
제로가 아닌 경우, 변환을 bool로하기 때문에 int 형 1. test
을 설정할 수 있도록 할당배열을 사용하는 C++ 회문문 프로그램
=
대
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Variables and Arrays
char Phrase[80];
char Reverse[80];
char* Palindrome = Reverse;
int i, j, test = 1;
cout << "Please enter a sentence to be reversed: ";
cin >> Phrase;
cin.getline(Phrase, 80);
int length = strlen(Phrase);
for(i = 0; i < (length/2); i++) // do a loop from 0 to half the length of the string
{
if(test == 1) // test for palindrome
{
if(Phrase[i] != Phrase[length-i-1]) // check if the characters match
{
test = 0; // if they don't set the indicator to false
}
}
else
{
break; // if it is not a palindrome, exit the for loop
}
}
if(test == 1) //test to print out the phrase if it's a palindrome
{
cout << "Phrase/Word is a Palindrome." << endl;
for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--)
{
*Palindrome = Phrase[j];
cout << "The reverse is: " << Reverse << endl << endl;
}
}
else
{
cout << "Phrase/Word is not a Palindrome." << endl;
}
system("Pause");
return 0;
}
자 이하로 작동하도록 코드를 수정 평등
를 사용하는 insted보십시오 나는 고정하는 두 경우 (테스트 == 1) 진술. 이제는 출력 결과가 바론돌인지 아닌지를 정확하게 알려주지 만 제대로 입력하면 출력되지 않습니다. –
@ T-Bird soo, 매번 회문이 아니십니까? – yizzlez
No. 입력은 모든 종류의 문자열을 허용합니다. 루프가 회문인지 테스트합니다. 두 번째 루프는 첫 번째가 참이면 역순으로 처리합니다. –