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