你如何打印出使用数组删除某些字符的回文
How do you print out a palindrome with certain characters removed using arrays?
我的程序应该测试回文,然后反向打印出来,但没有像'!'、'或'?'这样的字符。因此,输入的输入"女士我是亚当"将输出"女士",没有大写,空格或标点符号。我能够编写程序,但是您如何做其他部分来去除这些字符/大写字母?同样对于我的数组,当程序运行时,它会在打印出来时反向输出回文和回文之间的一堆奇数字符。我相信这是因为数组正在填充额外的字符空间,所以我将如何解决这个问题?
#include<iostream>
#include<string>
using namespace std;
int main()
{
//Variables and arrays
int const index = 30;
char Phrase[index];
char Reverse[index];
char* Palindrome = Reverse;
int i, j;
cout << "Please enter a sentence to be tested as a palindrome: ";
cin.getline(Phrase, 30);
int length = strlen(Phrase);
bool test = true;
for(i = 0; i != length/2; i++) //Loops from zero to half of the string
{
if(test) // if it is a palindrome so far
{
if(Phrase[i] != Phrase[length-i-1]) //To check if the characters match
{
test = false;
}
}
else
{
break;
}
}
if(test)
{
cout << endl << "Phrase/Word is a Palindrome." << endl << endl;
for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--)
{
*Palindrome = Phrase[j];
}
cout << "The phrase and reverse statement is: " << Reverse << endl << endl;
}
else
{
cout << endl << "Phrase/Word is not a Palindrome." << endl << endl;
}
system("Pause");
return 0;
}
您忘了将终止