从文本文件写入最长和最短的回文

本文关键字:回文 文件 文本 | 更新日期: 2023-09-27 18:36:01

>我正在尝试编写一个程序,该程序将从文本文件中的单词中写入最长和最短的回文。我的代码现在看起来像这样:

    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:'palindromy.txt");
        foreach (string line in lines)
        {
            char[] charArray = line.ToCharArray();
            for (int i = 0; i < 1; i++)
            {
                Array.Reverse(charArray);
                bool a = charArray.SequenceEqual(line);
                while(a == true)
                {                        
                    Console.WriteLine(line); /that will just write all palindroms
                    break;
                }
            }
        }
    }

我目前只是写所有的回文,但我只需要写最长和最短的回文。

从文本文件写入最长和最短的回文

与之前的答案类似,但有以下建议

  1. 初始化最长和最短。
  2. 比较时忽略大小写。仍然可以使用 SequenceEqual 而不是比较字符串来执行此操作。如果您知道不会将安娜与 annA 进行比较,则可能不需要。
  3. 在检查您是否找到最短时,您需要记住最短。长度为 0 开始,因此您需要存储找到的第一个回文,如果最短,则最短。长度==0。

    static void Main(string[] args)
    {
        var lines = System.IO.File.ReadAllLines(@"C:'palindromy.txt");
        string longest = string.Empty;
        string shortest = string.Empty;
        foreach (var line in lines)
        {
            if (line.Length == 0) continue;
            char[] charArray = line.ToCharArray();
            Array.Reverse(charArray);
            string reversedLine = new string(charArray);
            if (line.Equals(reversedLine, StringComparison.OrdinalIgnoreCase))
            {
                if (line.Length > longest.Length)
                {
                    longest = line;
                }
                if (line.Length < shortest.Length || shortest.Length == 0)
                {
                    shortest = line;
                }
            }
        }
        Console.WriteLine(longest);
        Console.WriteLine(shortest);
    }
    
 static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:'palindromy.txt");
        private string longest;
        private string shortest;
        foreach (string line in lines)
        {
            char[] charArray = line.ToCharArray();
            for (int i = 0; i < 1; i++)
            {
                Array.Reverse(charArray);
                bool a = charArray.SequenceEqual(line);
                if(a)
                {
                      if(a.Length > longest.length)
                         {
                              longest = a;
                         }
                      if(a.Length<shortest.length)
                         {
                            shortest = a;
                         }
                }
            }
        }
           Console.WriteLine(longest);
           Console.WriteLine(shortest);
    }

在我的头顶上,它看起来像这样。

如果使用一些 LINQ,则可以在很大程度上简化它:

string[] lines = System.IO.File.ReadAllLines(@"C:'palindromy.txt");
var palindromes = lines.Where(x => x == new string(x.ToCharArray()
                       .Reverse()
                       .ToArray()));
var shortest = palindromes.Aggregate((a, b) => a.Length < b.Length ? a : b);
var longest = palindromes.Aggregate((a, b) => a.Length > b.Length ? a : b);
Console.WriteLine("The shortest palindrome is {0}.", shortest);
Console.WriteLine("The longest palindrome is {0}.", longest);

这是一个简单的控制台应用程序,它完全符合您的要求。单词在 List ,因此您必须从文本文件中读取单词并自己将它们存储在List中。

using System;
using System.Collections;
using System.Collections.Generic;
namespace PalinDromes
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            List<string> words = new List<string> () { "anotherword","civic", "deified", "lemel", "pop" ,"noon"};
            string shortest = "";
            string longest = "";
            foreach (string word in words) 
            {
                if (isPalindrome (word)) 
                {
                    if (word.Length > longest.Length)
                        longest = word;
                    else if (shortest == "" || word.Length < shortest.Length)
                        shortest = word;
                }
            }
            Console.WriteLine ("Shortest: {0}", shortest);
            Console.WriteLine ("Longest: {0}", longest);
        }
        public static bool isPalindrome(string word)
        {
            return word == ReverseString (word);
        }
        public static string ReverseString(string s)
        {
            char[] arr = s.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
    }
}