1
0
oop-1-labs/Lab4/Lab4.FirstEqualLast/TaskUtils.cs

68 lines
2.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Lab4.FirstEqualLast
{
public class TaskUtils
{
/** Reads file and finds the number of words having same the first and
the last letters.
@param fin name of data file
@param punctuation punctuation marks to separate words */
public static int Process(string fin, string punctuation)
{
string[] lines = File.ReadAllLines(fin, Encoding.UTF8);
int equal = 0;
foreach (string line in lines)
if (line.Length > 0)
equal += FirstEqualLast(line, punctuation);
return equal;
}
/** Splits line into words and counts the words having same the first
and the last letters.
@param line string of data
@param punctuation punctuation marks to separate words */
private static int FirstEqualLast (string line, string punctuation)
{
string[] parts = Regex.Split(line.ToLower(), punctuation);
int equal = 0;
foreach (string word in parts)
if(word.Length > 0) // empty words at the end of line
if (word[0] == word[word.Length - 1])
equal++;
return equal;
}
/** Reads file and finds the number of words having same the first and
the last letters.
@param fin name of data file
@param punctuation punctuation marks to separate words */
public static int Process(string fin, char[] punctuation)
{
string[] lines = File.ReadAllLines(fin, Encoding.UTF8);
int equal = 0;
foreach (string line in lines)
if (line.Length > 0)
equal += FirstEqualLast(line, punctuation);
return equal;
}
/** Splits line into words and counts the words having same the first and the
last letters.
@param line string of data
@param punctuation punctuation marks to separate words */
private static int FirstEqualLast (string line, char[] punctuation)
{
string[] parts = line.ToLower().Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
int equal = 0;
foreach (string word in parts)
if (word[0] == word[word.Length - 1])
equal++;
return equal;
}
}
}