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

如题所述

using System.Text.RegularExpressions;
string str="aaajackbbbjacksccc";
string[] sArray=Regex.Split(str,"jack",RegexOptions.IgnoreCase);
//"jack"出现的次数:
int jackTimes=sArray.Length-1; 赞同0| 评论 2011-11-27 13:22 10网开 | 二级
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 出现的次数=" + mforeach循环就可以了,你想统计哪一个,然后找出他的ASCII码就可以了,然后遍历一下你要查找的地方 追问你说找出它的ASCII码?这用于统计字母出现的次数是可以的,但用于统计单词的个数好像行不通吧! 回答哦。不好意思啊,看错了,你可以这样,你统计某个单词时,可以使用空格,也是用遍历
if(遍历之后的变量=你要找的单词)

则记录一次

else
{}明白了吧 fgh
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-27
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 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 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;

本回答被提问者采纳
第2个回答  2011-11-27
using System.Text.RegularExpressions;
string str="aaajackbbbjacksccc";
string[] sArray=Regex.Split(str,"jack",RegexOptions.IgnoreCase);
//"jack"出现的次数:
int jackTimes=sArray.Length-1;
第3个回答  2011-11-27
foreach循环就可以了,你想统计哪一个,然后找出他的ASCII码就可以了,然后遍历一下你要查找的地方追问

你说找出它的ASCII码?这用于统计字母出现的次数是可以的,但用于统计单词的个数好像行不通吧!

追答

哦。不好意思啊,看错了,你可以这样,你统计某个单词时,可以使用空格,也是用遍历
if(遍历之后的变量=你要找的单词)

则记录一次

else
{}明白了吧

相似回答