简体   繁体   中英

Ignoring white space in palindrome

I am writing a program to check if a word is palindrome. The program itself works but when I enter this specific line of text "too bad - i hid a boot" it will not pick it up and ignore the white space and hyphen.

I looked at previous questions but I cant find a answer.

Is my Regex.Replace incorrect?

 string s, revs = "";
        Console.WriteLine("Please Enter Word");
        s = Console.ReadLine();
        string r = Regex.Replace(s , @"[^-\s]", "");

        for (int i = s.Length-1; i >= 0; i--)
        {
            revs += s[i].ToString();
        }

        if (revs == s)
        {
            Console.WriteLine("Entered string is palindrome \n String was {0} and reverse string was {1}", s, revs);
        }
        else
        {
            Console.WriteLine("String entered was not palindrome.");
        }
        Console.ReadKey();

Your code contains two errors:

  • This is the correct Regex condition @"(\\s|-)" yours replaces everything but the characters you want to remove
  • As jeffdot pointed out, you're replacing (incorrectly) but then testing against the original string

This considered, the correct code should be:

string s, revs = "";
        Console.WriteLine("Please Enter Word");
        s = Console.ReadLine();
        string r = Regex.Replace(s , @"(\s|-)", "");

        for (int i = r.Length-1; i >= 0; i--)
        {
            revs += r[i].ToString();
        }

        if (revs == r)
        {
            Console.WriteLine("Entered string is palindrome \n String was {0} and reverse string was {1}", s, revs);
        }
        else
        {
            Console.WriteLine("String entered was not palindrome.");
        }
        Console.ReadKey();

Except for the first WriteLine in which you should decide what to print (since revs doesn't contain spaces and the hyphen anymore).

Or if you want to just use String.Replace , you can use it twice with:

s=s.Replace(' ','');
s=s.Replace('-','');

You are assigning the result of that Regex.Replace call to string r and you are never referencing it. This is why your comparison is failing.

To make that more clear: you are removing whitespace and referencing that product as string r but you are reversing and testing against string s only.

Saverio also points out that your regex isn't going to work (I didn't test that myself).

You have:

    string r = Regex.Replace(s , @"[^-\s]", "");

    for (int i = s.Length-1; i >= 0; i--)
    {
        revs += s[i].ToString();
    }

Right there you should be iterating and reversing r instead of s --because s still has all its whitespace.

Further, as Saverio has said, why not just replace?

string r = s.Replace(" ", string.Empty).Reverse();

I haven't tried to compile that but as long as you are not using a very old version of the .NET framework that should work. It may require that you are using System.Linq to use the Reverse() extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM