C#
 Computer >> コンピューター >  >> プログラミング >> C#

文字列内の単語数をカウントするC#プログラム


最初に文字列を宣言しましょう-

string str = "Hello World!";

次に、文字列全体をループして、空白、タブ、または改行文字を見つけます-

while (a <= str.Length - 1) {
   if(str[a]==' ' || str[a]=='\n' || str[a]=='\t') {
      myWord++;
   }
   a++;
}
C#で文字列内の単語数をカウントするための完全なコードを見てみましょう。

using System;
public class Demo {
   public static void Main() {
      int a = 0 , myWord = 1;
      string str = "Hello World!";
      while (a <= str.Length - 1) {
         if(str[a]==' ' || str[a]=='\n' || str[a]=='\t') {
            myWord++;
         }
         a++;
      }
      Console.Write("Number of words in the string = {0}\n", myWord);
   }
}
出力
Number of words in the string = 2

  1. 指定された文字列のセットを使用して母音の数をカウントするPythonプログラム

    この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −文字列が与えられたので、与えられた文字列のセットを使用して母音の数を数える必要があります。 ここでは、文字列全体をトラバースして、各文字が母音であるかどうかを確認し、カウントをインクリメントします。 次に、以下の実装の概念を観察しましょう- 例 def vowel_count(str):    count = 0    #string of vowels    vowel = "aeiouAEIOU"   &nbs

  2. 指定された文字列のsetを使用して母音の数をカウントするPythonプログラム

    このプログラムでは、ユーザー入力文字列を指定します。この文字列の母音の数を数える必要があります。ここでは、Pythonでsetを使用します。 Setは、反復可能、変更可能で、重複する要素がない、順序付けされていないコレクションデータ型です。 例 Input : str1=pythonprogram Output : 3 アルゴリズム Step 1: First we use one counter variable which is used to count the vowels in the string. Step 2: Creating a set of vowels. Step