-1
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Palindrome
{
    class Program
    {
        static void Main(string[] args)
        {


            string filePath = @"C:\Users\Me\Desktop\Palindromes\palindromes.txt";
            //This gets the file we need

            var meStack = new Stack<string>();
            //this  creates the stack

            foreach (var item in File.ReadLines(filePath))
            {
                meStack.Push(item.ToUpper());
            }
            //for every item in the file, push onto the stack and make it upper case


            while (meStack.TryPop(out string Line))
            {
                reverseMe(Line);
            }
            //While every line in the stack is popped out, every line goes to the fucntion reverseMe






            static bool reverseMe(string Line)
            {
                return
                    Line == Line.Reverse();
            }
            //return true if line is the same as the line backwards or false if its not.


        }
    }

}

How do I get output? I have written comments to try and understand... but I am not getting a console output. I want the code to take in the file, put all the strings into a stack, and send every line in that stack to the reverseMe() function, which is a bool. The bool will see if the string is the same forward as it is backwards and if so it will return true or false. Basically my console is empty when I try to run this code.. What do I do?

Marvin
  • 65
  • 1
  • 8
  • What should the program do? I can't see any output. You, probably, want to add `Console.Write` or `Console.WriteLine` to have something written on the console – Dmitry Bychenko Mar 19 '20 at 20:36
  • This is a debugging problem, please learn how to use the debugger before asking a question, then when you cant understand something you bring that information to the question, what you expect to happen, what is happening, what line its happening. – TheGeneral Mar 19 '20 at 20:59
  • `static bool IsPalindrome(string input) { return input.SequenceEqual(input.Reverse()); }` should work, since you're trying to compare `IEnumerables` with `Reverse` – Rufus L Mar 19 '20 at 21:45

3 Answers3

1

There is a problem in the method reverseMe, The function Reverse gives you collection of char if applied on string, then you need to convert IEnumerable<char> to string by new string() or string.Concat(), like the following code:

static bool reverseMe(string Line)
{
    //deleting whitespaces, tabs
    Line = Regex.Replace(Line, @"\s+", "");

    return Line == new string(Line.Reverse().ToArray());
    //or
    //return Line == string.Concat(Line.Reverse());
    //or like Dmitry comment
    //return Line.SequenceEqual(Line.Reverse());
}

Calling reverseMe, and output result like : word is not palindrome

while (meStack.TryPop(out string Line))
{
    string isOrNotPalindrome = reverseMe(Line) ? string.Empty : "not";
    Console.WriteLine($"{Line} is {isOrNotPalindrome} palindrome");
}

Demo

bool isPalindrome1 = reverseMe("madam");
bool isPalindrome2 = reverseMe("nurses run");
bool isPalindrome3 = reverseMe("AaBbbBaAp");

Result

true
true
false

I hope this will help you fix the issue

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
  • 1
    or `return Line.SequenceEquals(Line.Reverse());` – Dmitry Bychenko Mar 19 '20 at 21:11
  • That's true, but ``SequenceEquals`` without ``s`` in the end :), i deleted also the white space, to test string like this``nurses run``. Thank you very much! – Mohammed Sajid Mar 19 '20 at 21:19
  • 1
    to delete *white*spaces (not just *ordinary* spaces, but tabs, new lines etc.): `Line = Regex.Replace(Line, @"\s+", "");` – Dmitry Bychenko Mar 19 '20 at 21:25
  • @Marvin if i understand you correctly, i updated my answer, by adding ``while`` loop. if it worked for you, then please upvote and accept the answer, so that others would find it useful – Mohammed Sajid Mar 19 '20 at 22:08
1

Let's start from the problem; I assume that you want to scan all file's lines and print out if the line is a palindrom.

First, we need to implement IsPalindrom method:

private static bool IsPalindrom(string value) {
  if (null == value)
    return false; // or true, ot throw ArgumentNullException

  // We have to prepare the string: when testing for palindrom
  //  1. Let's ignore white spaces (' ', '\r', '\t' etc.)
  //  2. Let's ignore punctuation  (':', ',' etc.)
  //  3. Let's ignore cases        (i.e. 'M' == 'm') 
  // So "Madam, I'm Adam" will be a proper palindrom
  value = string.Concat(value
    .Where(c => !char.IsWhiteSpace(c))
    .Where(c => !char.IsPunctuation(c))
    .Select(c => char.ToUpperInvariant(c)));

  // Instead of Reversing we can just compare:
  // [0] and [Length - 1] then [1] and [Length - 2] etc.
  for (int i = 0; i < value.Length / 2; ++i)
    if (value[i] != value[value.Length - 1 - i])
      return false; // we have a counter example: value is NOT a palidrom

  // Value has been scanned, no counter examples are found
  return true;    
}

Time to write Main method:

static void Main(string[] args) {
  string filePath = @"C:\Users\Me\Desktop\Palindromes\palindromes.txt";

  var result = File
    .ReadLines(filePath)
    .Where(line => !string.IsNullOrWhiteSpace(line)) // let's skip empty lines
    .Select(line => $"{(IsPalindrom(line) ? "Palindrom" : "Not a palindrom")}: \"{line}\"");

  // Do not forget to print result on the Console:
  foreach (var record in result)   
    Console.WriteLine(record);

  // Pause to have a look at the outcome (wait for a key to be pressed)
  Console.ReadKey(); 
} 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
-2

Uhm there isn't a Console.WriteLine() to do any actual output after getting results from the reverseMe() function.

  • This is not an answer. Please use the comment section of the question (or an answer) for comments. Also note that values can be examined in other ways, such as with breakpoints in the debugger. – Rufus L Mar 19 '20 at 21:49