Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class ExtractPalindromesFromText
- {
- static void Main(string[] args)
- {
- string str = "Some text, ABBA alfa test txt bob soso potop php dhl";
- string[] splitByWhitespace = str.Split(new char[]{' ', ',', '"'}, StringSplitOptions.RemoveEmptyEntries);
- List<string> palindromes = new List<string>();
- foreach (var word in splitByWhitespace)
- {
- bool isPalindrom = true;
- for (int i = 0; i < word.Length / 2; i++)
- {
- if (word[i] != word[word.Length - 1 - i])
- {
- isPalindrom = false;
- break;
- }
- }
- if (isPalindrom)
- {
- palindromes.Add(word);
- }
- }
- foreach (var words in palindromes)
- {
- Console.WriteLine(words);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement