C# 程式用於計算字串中單詞的數量
我們首先宣告字串
string str = "Hello World!";
現在遍歷整個字串並查詢空格、製表符或換行符
while (a <= str.Length - 1) {
if(str[a]==' ' || str[a]=='
' || 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]=='
' || str[a]=='\t') {
myWord++;
}
a++;
}
Console.Write("Number of words in the string = {0}
", myWord);
}
}輸出
Number of words in the string = 2
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP