文字列内の単語のインデックスを検索するC#プログラム
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
}; ここで、ue IndexOf()メソッドを使用して、「Mat」という単語のインデックスを検索します-
Array.IndexOf(str, "Mat");
以下はコードです-
例
using System;
public class Demo {
public static void Main() {
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
};
Console.WriteLine("Our Array =");
for (int i = 0; i < str.Length; i++) {
string res = str[i];
Console.WriteLine(res);
}
int findIndex = Array.IndexOf(str, "Mat");
Console.Write("Element Mat found at the following index: ");
Console.WriteLine(findIndex);
}
} 出力
Our Array = Cat Mat Rat Element Mat found at the following index: 1
-
Pythonで指定された文字列の最初の繰り返し文字のインデックスを見つけるプログラム
文字列sがあるとすると、その中の最初の繰り返し文字のインデックスを見つける必要があります。繰り返し文字が見つからない場合は、-1を返します。 したがって、入力が「abcade」のような場合、「a」はインデックス3に再び存在するため、出力は3になります。 これを解決するには、次の手順に従います- マップ文字を定義する 0からsのサイズの範囲のiについては、 文字でs[i]の場合、 私を返す それ以外の場合、 chars [s [i]]:=chars [s [i]] + 1 戻り値-1 理解を深めるために、次の実装を見てみましょう- 例 from collections
-
Pythonで文字列の最初に繰り返される単語を見つけますか?
1つの文字列が与えられます。私たちのタスクは、与えられた文字列の最初に繰り返される単語を見つけることです。この問題を実装するために、Pythonコレクションを使用しています。コレクションから、Counter()メソッドを取得できます。 アルゴリズム Repeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: travers