Last active
July 10, 2020 18:04
-
-
Save sivinnguyen/3eb5a23b0d0edc786b7eb39d88664719 to your computer and use it in GitHub Desktop.
C# Practice 190714
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Đề: Cho 1 list string đếm xem input nhập vào match bao nhiêu lần. | |
Ví dụ: | |
String[] arr = | |
{ | |
"tôi thấy hoa vàng trên cỏ xanh", | |
"hôm nay tôi thấy hoa vàng", | |
"hoa vàng trên cỏ xanh, hoa vàng trên cỏ xanh" | |
} | |
Expectation: | |
input = "cỏ xanh", output = 3 | |
input = "hoa vàng trên cỏ xanh", output = 3 | |
input = "hoa xanh", output = 0 | |
*/ | |
using System; | |
using System.Text.RegularExpressions; | |
namespace pr190714 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Clear(); | |
Console.WriteLine("=== Bai tap tim chuoi ===" + Environment.NewLine); | |
String[] arr = | |
{ | |
"toi thay hoa vang tren co xanh", | |
"hom nay toi thay Hoa vang", | |
"HOA vang tren co xanh, hoa Vang tren co xanh" | |
}; | |
Version2(arr); | |
Console.Clear(); | |
} | |
/* | |
* VERSION 2: Sử dụng Regex.Matches | |
* Highlight kết quả tìm kiếm | |
*/ | |
static void Version2(String[] arr) | |
{ | |
int count; | |
string input; | |
String[] result; | |
displayStringsList(arr); | |
do | |
{ | |
Console.WriteLine(); | |
input = getString("Nhap chuoi can tim: "); | |
matchString(input, arr, out count, out result); | |
Console.WriteLine | |
( | |
$"-> Tim duoc {count} cum tu \"{input}\"!" | |
+ Environment.NewLine | |
); | |
highlightResult(result); | |
Console.Write | |
( | |
Environment.NewLine | |
+ "Ban co muon tiep tuc tim kiem? An <Y> de tiep tuc, phim khac de thoat... " | |
); | |
} while (Console.ReadKey().Key == ConsoleKey.Y); | |
} | |
/* | |
* VERSION 1: Sử dụng String.IndexOf() | |
*/ | |
static void Version1(String[] arr) | |
{ | |
displayStringsList(arr); | |
string input; | |
do | |
{ | |
Console.WriteLine(); | |
input = getString("Nhap chuoi can tim: "); | |
matchString(input, arr); | |
Console.Write | |
( | |
Environment.NewLine | |
+ "Ban co muon tiep tuc tim kiem? An <Y> de tiep tuc, phim khac de thoat... " | |
); | |
} while (Console.ReadKey().Key == ConsoleKey.Y); | |
} | |
/// <summary> | |
/// Hiển thị danh sách chuỗi. | |
/// </summary> | |
/// <param name="arr"></param> | |
static void displayStringsList(String[] arr) | |
{ | |
Console.WriteLine("{"); | |
for (int i = 0; i < arr.Length; i++) | |
{ | |
Console.WriteLine("\t" + arr[i]); | |
} | |
Console.WriteLine("}"); | |
} | |
/// <summary> | |
/// Tìm chuỗi. | |
/// </summary> | |
/// <param name="input">Chuỗi cần tìm.</param> | |
/// <param name="arr">Danh sách chuỗi.</param> | |
static void matchString(string input, String[] arr) | |
{ | |
int count = 0; | |
for (int i = 0; i < arr.Length; i++) | |
{ | |
int j = 0; | |
while ( (j = arr[i].IndexOf(input, j)) != -1) | |
{ | |
j += input.Length; | |
count++; | |
} | |
} | |
Console.WriteLine($"-> Tim duoc {count} cum tu \"{input}\"!"); | |
} | |
/// <summary> | |
/// Tìm kiếm sử dụng Regex. | |
/// </summary> | |
/// <param name="input">Chuỗi cần tìm</param> | |
/// <param name="arr">Danh sách chuỗi</param> | |
/// <param name="count">Số lượng tìm được</param> | |
/// <param name="result">Kết quả tìm kiếm</param> | |
static void matchString(string input, String[] arr, out int count, out String[] result) | |
{ | |
result = new String[arr.Length]; | |
count = 0; | |
for (int i = 0; i < arr.Length; i++ ) | |
{ | |
// Tạo một pattern tìm kiếm, không phân biệt chữ hoa chữ thường | |
Regex regExp = new Regex(input, RegexOptions.IgnoreCase); | |
// Đếm kết quả | |
count += regExp.Matches(arr[i]).Count; | |
// Gán label, dùng MatchEvaluator để không phân biệt chữ hoa chữ thường | |
result[i] = regExp.Replace(arr[i], new MatchEvaluator(replaceKeywords)); | |
}; | |
} | |
/// <summary> | |
/// Highlight kết quả tìm kiếm | |
/// </summary> | |
/// <param name="result"></param> | |
static void highlightResult(String[] result) | |
{ | |
Console.WriteLine("{"); | |
for (int i = 0; i < result.Length; i++) | |
{ | |
Console.Write("\t"); | |
char[] c = result[i].ToCharArray(); | |
for (int j = 0; j < c.Length; j++) | |
{ | |
if (c[j] == '[') | |
{ | |
Console.ForegroundColor = ConsoleColor.Black; | |
Console.BackgroundColor = ConsoleColor.DarkYellow; | |
} | |
else if (c[j] == ']') | |
{ | |
Console.ResetColor(); | |
} | |
else | |
{ | |
Console.Write(c[j]); | |
} | |
} | |
Console.WriteLine(); | |
} | |
Console.WriteLine(Environment.NewLine + "}"); | |
} | |
/// <summary> | |
/// Label | |
/// </summary> | |
/// <param name="m"></param> | |
/// <returns></returns> | |
static string replaceKeywords(Match m) | |
{ | |
return "[" + m.Value + "]"; | |
} | |
/// <summary> | |
/// Nhận chuỗi từ console. Không chấp nhận chuỗi rỗng. | |
/// </summary> | |
/// <param name="msg">Thông báo</param> | |
/// <returns></returns> | |
static string getString(string msg) | |
{ | |
string input; | |
do | |
{ | |
Console.Write(msg); | |
input = Console.ReadLine(); | |
} while (String.IsNullOrEmpty(input)); | |
return input; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment