如何检查不同的字符串是否是回文? [英] How to check if different strings are a palindrome?

查看:91
本文介绍了如何检查不同的字符串是否是回文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何检查单个字符串以描述它们是否是回文。该程序将两个字符串视为一个,因此当其中一个字符串实际显示时,它显示它不是一个回文。



我尝试了什么:



I'm not sure how to check individual strings to depict if they're palindromes or not. The program treats both strings as one therefore displaying 'it is not a palindrome' when one of the strings actually is.

What I have tried:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Palindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack list = new Stack();
            string ch;
            string value1= "eye";
            string value2 = "view";

            string word = string.Format("{0}\n{1}\n", value1, value2);                                                              
            bool palindrome = true;

            for (int x = 0; x < word.Length; x++)
                list.push(word.Substring(x, 1));
            int pos = 0;

            while (list.count > 0)
            {
                ch = list.pop().ToString();
                if (ch != word.Substring(pos, 1))
                {
                    palindrome = false;
                    break;
                }
                pos++;
            }
            if (palindrome)
                Console.WriteLine(word + " \nis a palindrome.");
            else
                Console.WriteLine(word + " \nis not a palindrome.");
            Console.Read();
        }

        class Stack
        {
            private int index;
            private ArrayList list;
            public Stack()
            {
                list = new ArrayList();
                index = -1;
            }
            public int count
            {
                get
                {
                    return list.Count;
                }
            }
            public void push(object item)
            {
                list.Add(item);
                index++;
            }
            public object pop()
            {
                object obj = list[index];
                list.RemoveAt(index);
                index--;
                return obj;
            }
            public void clear()
            {
                list.Clear();
                index = -1;
            }
            public object peek()
            {
                return list[index];
            }
      
        }
    }
}

推荐答案

你是实际检查 word 的值,看它是否是回文。
You are actually checking the value of word to see if it is a palindrome.
eye
view

不是回文。如果你想检查那些单词,那么你必须进行一些处理来将单词分开,例如用 \ n 拆分并在循环中处理该数组中的每个结果。



如果您将代码更改为

is not a palindrome. If you want to check those individual words then you have to put in some processing to separate the words out e.g split by \n and handle each result in that array in a loop.

If you change your code to

string word = value1;

那么它会正确地指出这是一个回文。



也许最着名的回文是女士,我是亚当。您的代码不会将其视为回文因为:

1.您没有删除或处理标点符号,例如'

2.如果 M 与$相等,则不处理差异c $ c> m 用于寻找回文。

3.您正在插入 \ n 您的测试数据。

then it correctly notes this is a palindrome.

Perhaps the most famous palindrome is "Madam, I'm Adam". Your code will not view this as a palindrome because:
1. You are not removing or handling punctuation marks such as ', , etc
2. You are not handling differences in case M should be "equal" to m for the purposes of finding a palindrome.
3. You are inserting \n into your test data.


如果您不理解您的代码正在做什么或为什么会这样做,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个bug。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


尝试使用linq。类似

Try using linq. Something like

var names=new List<string>(){"level","eve","tub","Eye","Racecar","motor"};
var palindromes = names.Select(n => n.ToLower().ToCharArray())
.Where(n => n.Reverse().SequenceEqual(n));
foreach (var p in palindromes)
{
 Console.WriteLine(p);
}


这篇关于如何检查不同的字符串是否是回文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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