你如何打印出使用数组包含某些字符的回文中删除? [英] How do you print out a palindrome with certain characters removed using arrays?

查看:132
本文介绍了你如何打印出使用数组包含某些字符的回文中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序应该测试回文,然后打印出来反转,但没有像字符'!','或'?'。所以输入的输入夫人,我是亚当将输出madamimadam没有大写,空格或标点符号。我能写的程序,但你怎么做,其他部分拿走这些字符/大小写?也为我的数组,当程序运行它输出一串奇怪的字符在回文和反向回文之间时,它打印出来。我相信这是因为数组是多余字符空格填充,所以我将如何解决这个问题呢?

 #包括LT&;&iostream的GT;
#包括LT&;串GT;
使用命名空间std;诠释的main()
{
    //变量和数组
    INT常量指数= 30;
    焦炭短语[指数]
    字符反向[指数]
    字符*回文=反转;
    INT I,J;    COUT<< 请输入一个句子进行测试作为一个回文;
    cin.getline(词组,30);
    INT长度= strlen的(短语);    布尔测试= TRUE;    为(ⅰ= 0;!I =长度/ 2;我++)//从零环路到字符串半
    {
        如果(测试)//如果它是一个回文迄今
        {
            如果(短语[I]!=乐句[长度-I-1])//要检查的字符一致
            {
                测试= FALSE;
            }        }
        其他
        {
            打破;
        }
    }    如果(测试)
    {
        COUT<< ENDL<< 这句/字是回文。 << ENDL<< ENDL;
        为(J = strlen的(词组) - 1; J> = 0;回文++,j--)
        {
            *回文=短语[J]。
        }
        COUT<< 这句话和反向的说法是:<<反向<< ENDL<< ENDL;
    }
    其他
    {
        COUT<< ENDL<< 这句/字不是一个回文。 << ENDL<< ENDL;
    }    系统(暂停);
    返回0;
}


解决方案

 的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;&cctype GT;
    使用命名空间std;诠释的main()
{
//变量和数组
INT常量指数= 80;
焦炭短语[指数]
焦炭NewPhrase [指数]
INT I,J,K,L;
布尔测试= TRUE;//短语/字提示用户
COUT<< 请输入一个句子进行测试作为一个回文;
cin.getline(词组,80);//使所有的小写,删除空间,并复制到一个新的数组'NewPhrase
对于(K = 0,L = 0; K< = strlen的(短语); K ++)
{
    如果((短语[K] ='')及!及(ispunct(短语[K])== FALSE))
    {
        NewPhrase [1] = tolower的(这句[K]);
        升++;
    }
}INT长度= strlen的(NewPhrase); //获取短语的长度对于(i = 0,J =长度-1; I<焦耳;我++,j--)
{
    如果(测试)//测试以查看是否短语是回文
    {
        如果(NewPhrase [I]!= NewPhrase [J]。)
            测试= FALSE;
    }
    其他
        打破;
}如果(测试)
{
    COUT<< ENDL<< 这句/字是回文。 << ENDL<< ENDL;
    COUT<< 回文是:<< NewPhrase<< ENDL<< ENDL;
}
其他
    COUT<< ENDL<< 这句/字不是一个回文。 << ENDL<< ENDL;系统(暂停);
返回0;
}

My program is supposed to test for a palindrome and then print it out in reverse but without characters like '!', ', or '?'. So the entered input "madam I'm adam" would output "madamimadam" with no capitalization, blanks, or punctuation. I was able to write the program but how do you do that other part to take away those characters/capitalization? Also for my array, when the program runs it outputs a bunch of odd characters in between the palindrome and the palindrome in reverse when it prints out. I believe this is because the array is filling in the extra character spaces so how would I fix that as well?

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

解决方案

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

这篇关于你如何打印出使用数组包含某些字符的回文中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆