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 婚前女儿生日祝福语简短