c#实现,统计一句英文句子中某个单词出现的次数.

问题描述:

c#实现,统计一句英文句子中某个单词出现的次数.
1个回答 分类:综合 2014-09-21

问题解答:

我来补答
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();
}
}
}
再问: 要在window窗体实现,并且要使用indexof()方法,这样的话你知道不
再答: 你把题目说具体些,要在哪个控件上显示数据
再问: 题目是:编写Windows应用程序,在文本框textBox1中输入一个字符串,单击“统计”按钮,在label1中显示输出字符串“the”的出现次数(不区分大小写),使用循环结构、Length属性和IndexOf()方法。
再答: 上面是英文句子,而这里是字符串,到底是个英文句子(有空格的Look at the ball),还是随便写的,如 dfl j;thedfal; thesdjf 这里的the的个数
再问: 我已经写出来啦,不过还是要谢谢你! int[] a = new int[50]; string str = textBox1.Text.ToLower (); int m = 0; for (int i = 0; i < str.Length; i++) { int n = str.IndexOf("the") +3; if (n > i) m++; str = str.Substring(n); } label1.Text = "the 出现的次数=" + m;
再答: 哥们,不好意思,今天才看到,但是我发现你的还是有问题的,如果你输入的内容没有the,就会不对,如ddgga,改成如下: string str = textBox1.Text.ToLower(); int m = 0; for (int i = 0; i < str.Length; i++) { int n = str.IndexOf("the") + 3; if (str.IndexOf("the") == -1) break; if (n > i) m++; str = str.Substring(n); } label1.Text = "the 出现的次数=" + m; 下面是我做的,你可以参考下: int indexnumber = 0; string str = textBox1.Text.ToLower(); int count= 0; for (int i = 0; i < str.Length; i++) { indexnumber = str.IndexOf("the",indexnumber ); if (indexnumber == -1) break; indexnumber++; count++; } label1.Text = "the 出现的次数=" + count;
 
 
展开全文阅读
剩余:2000
上一页:14,16
下一页:对一下