你如何打印出使用数组删除某些字符的回文

How do you print out a palindrome with certain characters removed using arrays?

本文关键字:删除 数组 字符 回文 何打印 打印      更新时间:2023-10-16

我的程序应该测试回文,然后反向打印出来,但没有像'!'、'或'?'这样的字符。因此,输入的输入"女士我是亚当"将输出"女士",没有大写,空格或标点符号。我能够编写程序,但是您如何做其他部分来去除这些字符/大写字母?同样对于我的数组,当程序运行时,它会在打印出来时反向输出回文和回文之间的一堆奇数字符。我相信这是因为数组正在填充额外的字符空间,所以我将如何解决这个问题?

#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;
}

您忘了将终止添加到 Reverse 。在循环后添加*Palindrome = ''

只需使用单独的迭代器即可。

而不是

for(i = 0; i != length/2; i++)

for(i = 0, j = length-1; i < j; i++, j--) 

然后对于你的 if 语句,你可以做类似的事情

if(test) // if it is a palindrome so far
{
    while(!isalpha(Phrase[i]) && i < j) { i++; }
    while(!isalpha(Phrase[j]) && i < j) { j--; }
    if(Phrase[i] != Phrase[j]) //To check if the characters match
    {
        test = false;
    }

这将导致程序忽略任何字符,而不是字母。如果你想让它也能识别数字,你可以使用 isalphanum。

#include <iostream>
#include <string>
#include <cctype>
    using namespace std;
int main()
{
//Variables and arrays
int const index = 80;
char Phrase[index];
char NewPhrase[index];
int i, j, k, l;
bool test = true;
//Prompt user for the phrase/word
cout << "Please enter a sentence to be tested as a palindrome: ";
cin.getline(Phrase, 80);
//Make everything lowercase, delete spaces, and copy that to a new array 'NewPhrase'
for(k = 0, l = 0; k <= strlen(Phrase); k++)
{
    if((Phrase[k] != ' ') && (ispunct(Phrase[k]) == false))
    {
        NewPhrase[l] = tolower(Phrase[k]);
        l++;
    }
}
int length = strlen(NewPhrase); //Get the length of the phrase
for(i = 0, j = length-1; i < j; i++, j--)
{
    if(test) //Test to see if the phrase is a palindrome
    {
        if(NewPhrase[i] != NewPhrase[j])
            test = false;
    }
    else
        break;
}
if(test)
{
    cout << endl << "Phrase/Word is a Palindrome." << endl << endl;
    cout << "The Palindrome is: " << NewPhrase << endl << endl;
}
else
    cout << endl << "Phrase/Word is not a Palindrome." << endl << endl;
system("Pause");
return 0;
}