c#实现,统计一句英文句子中某个单词出现的次数.
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/05 18:36:31
![c#实现,统计一句英文句子中某个单词出现的次数.](/uploads/image/z/2693927-47-7.jpg?t=c%23%E5%AE%9E%E7%8E%B0%2C%E7%BB%9F%E8%AE%A1%E4%B8%80%E5%8F%A5%E8%8B%B1%E6%96%87%E5%8F%A5%E5%AD%90%E4%B8%AD%E6%9F%90%E4%B8%AA%E5%8D%95%E8%AF%8D%E5%87%BA%E7%8E%B0%E7%9A%84%E6%AC%A1%E6%95%B0.)
c#实现,统计一句英文句子中某个单词出现的次数.
c#实现,统计一句英文句子中某个单词出现的次数.
c#实现,统计一句英文句子中某个单词出现的次数.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s,s1,s2;
int count = 0;
Console.WriteLine ("请输入你要查找的那个单词的句子:");
string str= Console.ReadLine ();
Console.WriteLine ("请输入你要查找的那个单词:");
string serchword= Console.ReadLine ();
int sbcount = serchword.Count();
for (int i = 0; i < str.Count(); i++)
{
if ( i == 0)
{
s1 = " ";
s2 = str.Substring(i + sbcount ,1);
s = str.Substring(i,sbcount);
if (s1 == " " && s2 == " ")
{
if (s == serchword)
count++;
}
}
if (i + serchword.Count() < str.Count() && i != 0)
{
s1 = str.Substring(i - 1,1);
s2 = str.Substring(i + sbcount-1 ,1);
s = str.Substring(i,sbcount);
if (s1 == " " && s2 == " ")
{
if (s == serchword)
count++;
}
}
if (i + serchword.Count() == str.Count() )
{
s1 = str.Substring(i - 1,1);
s2 = " ";
s = str.Substring(i,sbcount);
if (s1 == " " && s2 == " ")
{
if (s == serchword)
count++;
}
break;
}
}
Console.WriteLine(count );
Console.ReadLine();
}
}
}