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