C#程序计算字符串中的单词数
让我们首先声明字符串-
string str = "你好,世界!";
现在遍历完整的字符串,找到空白或制表符或换行符-
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 = "你好,世界!";
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
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话