确定用户输入是否为回文

时间:2017-02-22 08:06:43

标签: c++11 palindrome

#include <iostream>
#include <cstring>
#include <string>
#include <ctype.h>
using namespace std;

//declare methods
void transform(char *, char *);
bool testPalindrome(char *);


int main()
{
    //declare c-strings and a boolean variable
    char str[80];
    char myStr[100];
    char defaultValue[] = "0";
    bool done = true;

    //continously ask a user to enter a string until the user enters 0
    while (done)
    {
        cout << "Enter a string (0 to exit the program): ";
        cin.getline(str, 80);
        if (strcmp(str,defaultValue) == 0)
        {
            done = false;
        }
        else
        {
            //convert all lowercase letters of user input to uppercase
            transform(str, myStr);

            //test if user input is the same when read backward and forward
            if (testPalindrome(myStr))
            {
                cout << "It is a palindrome." << endl;
            }
            else
            {
                cout << "It is not a palindrome." << endl;
            }
        }
    }
    system("pause");
    return 0;
}
/*
This method converts all lowercase letters into uppercase letters
as well as removes characters that are not numbers or letters.
The new string will be stored in the C-string testStr
*/
void transform(char * raw, char * testStr)
{
    int length = strlen(raw);
    int j = 0;

    //convert all lowercase letters to uppercase letters if current letter      is lowercase
for(int i = 0; i < length; i++)
{
    if (islower(raw[i])) 
    {
        raw[i] = toupper(raw[i]);
    }

}
//copy user input, remove all characters that are letters or numbers
for (int k = 0; k < length; k++)
{
    if ((isalpha(raw[k]) || isdigit(raw[k])))
    {
        *(testStr + k) = *(raw + k);
    }
    else if (isspace(raw[k]))
    {
        const char * current = &raw[k];
        remove(current);
    }
    j++;
}
*(testStr + j) = '\0';
}
/*
This method determines if the user input can be read the same backward or       forward.
will take a parameter of a pointer variable, which refers to
memory address of the c-string
*/
bool testPalindrome(char * str)
    {
        int test = 1;
        bool flag = true;
        int length = strlen(str) - 1;
        int n = length;
       for (int i = 0; i <= length && n >= 0; i++, n--) 
       {
             if (toupper(str[i]) == toupper(str[n]))
             {
                  flag = true;
             }
             else
             {
                 flag = false;
             }
       }
    return flag;
}

在这个程序中,我试图显示用户输入是否是回文。我有5个字符串要测试:

  

雷达

     

太热了以至于

     

女士!我是亚当

     

一个男人,一个计划,一个运河 - 巴拿马

     

Doc,请注意,我不同意!快速永远不会阻止肥胖;我吃鳕鱼

出于某种原因,我的程序通过了这5个案例中的4个。另外,我测试了非回文,它似乎工作得很好。下图显示了5个用户输入字符串的结果:

https://i.stack.imgur.com/cYmHe.png

正如您在图像中看到的那样,字符串"A man, A plan, A canal-Panama"给出了相反的结果,而其他字符串提供了预期的结果。我的代码在哪里导致这个?建议性和/或建设性的批评会非常有用。

1 个答案:

答案 0 :(得分:0)

对于您的示例代码,主要问题在于您在transform方法中的第二个for循环。这是固定版本。

// copy str, remove all characters that are NOT letters or numbers
for (int k = 0; k < length; k++)
{
    if ((isalpha(raw[k]) || isdigit(raw[k])))
    {
        *(testStr + j++) = *(raw + k);
    }
}

顺便说一句,您的代码中还有其他几个问题,我建议您查看leetcode here中的好解决方案。

相关问题