c#实现,统计一句英文句子中某个单词出现的次数.
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/03 03:44:52
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();
}
}
}