特定の文字列を定義済みの文字列と比較し、挿入エラー、削除エラー、転置または置換エラーが発生したかどうかを判断する必要があるプログラムがあります。
たとえば、単語dogがユーザーに提示され、ユーザーがdogsまたはdogeを送信した場合、挿入エラーが発生したことをユーザーに通知する必要があります。
どうすればいいですか?
頭のてっぺんから外れていますが、これで始められると思います:
Insertion
とてもシンプルなDeletion
はずです。文字列の長さを確認するだけです。
if(originalString.Length > newString.Length)
{
//deletion
}
else if(originalString.Length < newString.Length)
{
//insertion
}
を検出するtransposition
には、長さが一致するかどうかを確認します。一致する場合は、2 つの文字列から 2 つを作成できますList<char>
。次に、以下の式を使用して一致するかどうかを確認します
bool isTransposed = originalList.OrderBy(x => x).SequenceEquals(newList.OrderBy(y => y));
を検出するには、ハミング距離substitution
を使用して、それが 0 より大きいかどうかを確認できます。
エラーかどうかを確認するには、次のように、個々のエラーの種類ごとにメソッドを記述する必要があるでしょう。
bool IsInsertionError(string expected, string actual) {
// Maybe look at all of the chars in expected, to see if all of them
// are there in actual, in the correct order, but there's an additional one
}
bool IsDeletionError(string expected, string actual) {
// Do the reverse of IsInsertionError - see if all the letters
// of actual are in expected, in the correct order,
// but there's an additional one
}
bool IsTransposition(string expected, string actual) {
// This one might be a little tricker - maybe loop through all the chars,
// and if expected[i] != actual[i], check to see if
// expected[i+1] = actual[i] and actual[i-1]=expected[i]
// or something like that
}
個々のルールをすべて構築したら、最初に通常の同等性をチェックし、一度に 1 つずつ起動します。
このような問題を小さなコンポーネントに分解し、簡単な問題がたくさんできたら、それらを 1 つずつ解決する必要があります。
としてfunction
を取るを作成することをお勧めします。多かれ少なかれこのように見えます。好きな場所で使用してください。parameter
input sting
function
function
private void CheckString(string userString)
{
string predefinedString = "dog";
if (userString == predefinedString)
{
// write your logic here
}
else
{
MessageBox.Show("Incorrect word"); // notify the users about incorrect word over here
}
}