Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using System.Text;
- namespace unideb_mother_bear
- {
- // Mother Bear (example)
- // https://www.inf.unideb.hu/progcont/exercises.html?pid=10945
- class Program
- {
- const string EATYOU = "Uh oh...";
- const string NOTEATYOU = "You won't be eaten!";
- static void Main(string[] args)
- {
- char[] unneededChars = new char[] { ',', ' ', '.', '!', '?' };
- string[] lines = ReadFromFile(new StreamReader("in.txt"));
- lines = RemoveCharacters(lines, unneededChars);
- string[] messages = new string[lines.Length];
- for (int i = 0; i < lines.Length; i++)
- {
- bool result = IsPalindrome(lines[i]);
- Console.WriteLine("{0} {1} palindrome!", lines[i], result ? "is" : "is not");
- messages[i] = result ? NOTEATYOU : EATYOU;
- }
- WriteToFile(messages, new StreamWriter("out.txt"));
- Console.WriteLine("...written in file!");
- Console.ReadLine();
- }
- static string[] ReadFromFile(StreamReader sr)
- {
- string content = sr.ReadToEnd();
- content = content.Substring(0, content.IndexOf("DONE"));
- return content.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
- }
- static string[] RemoveCharacters(string[] lines, char[] removableChars)
- {
- for (int i = 0; i < lines.Length; i++)
- {
- // Remove upper chars
- lines[i] = lines[i].ToLower();
- List<char> chars = new List<char>();
- // Split the string to characters.
- char[] source = lines[i].ToCharArray();
- for (int j = 0; j < source.Length; j++)
- {
- for (int k = 0; k < removableChars.Length; k++)
- {
- if (source[j] == removableChars[k])
- {
- // We are sure that this char is not needed.
- break;
- }
- if (k == removableChars.Length - 1)
- {
- // Iteration done, no break happened: we add this char to the list!
- chars.Add(source[j]);
- }
- // We continue iterating.
- }
- }
- lines[i] = new string(chars.ToArray());
- }
- return lines;
- }
- static bool IsPalindrome(string str)
- {
- for (int i = 0; i < str.Length / 2; i++)
- {
- if (str[i] != str[str.Length - i - 1])
- {
- return false;
- }
- }
- return true;
- }
- static void WriteToFile(string[] lines, StreamWriter sw)
- {
- if (sw != null)
- {
- foreach (string line in lines)
- {
- sw.WriteLine(line);
- }
- sw.Close();
- }
- else throw new ArgumentNullException("StreamWriter is null!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment