问题:键盘输入如“ABefgt”的字母串,使用C#编程实现计算每个字母出现次数的程序。当键盘输入字符串后,如何遍历和比较字符串中每个字符是否相等呀?(本人认真学习当中)
希望大家用代码支持我下,谢了
希望大家用代码支持我下,谢了
解决方案 »
- 用C#画圆与矩形
- win7旗舰版安装Visual Studio 2010 Ultimate失败,求救!
- 新手提问 如何在自定义的静态方法中 调用窗体控件的属性值
- 数组冒泡排序后,如何去掉最大值和最小值再算平均分?
- 狂郁闷:(C#)自己编程打印localreport时,打出的报表边距发生偏移,分别向右和向下偏移了0.4cm,不知如何解决此问题
- 想设置等待时间 但是不要程序休眠
- 求助 串口实时数据发送
- 求助?为什么WPF中dataTable绑定的数据 不能更新
- 如何用泛型判断字段类型为数组
- 邮箱的问题
- 在c#中File、FileInfo、Directory、DirectoryInfo类之间的区别?
- 谁知道在ADO.NET Entity Framework 外键为什么不能映射到实体属性上?
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
int count = 0;
SortedList list = new SortedList();
string[] str = { "abcd", "我们dcag", "有本事来啊啊hello", "呵呵的hi", "你name", "屁compute", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊hello" }; Console.WriteLine( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff").ToString()); string time1 = DateTime.Now.ToString();
for (int i = 0; i < str.Length; i++)
{
for (int j = 0; j < str[i].Length; j++)
{
for (int k =i; k < str.Length; k++)
for (int l = 0; l < str[k].Length; l++)
{
if (!ht.Contains(str[i][j].ToString()))
{
if (str[i][j] == str[k][l])
{
count++;
str[k].Remove(l,1);
}
}
else
{
break;
}
} if (!ht.Contains(str[i][j].ToString()))
{
ht.Add(str[i][j].ToString(), count.ToString());
list.Add(str[i][j].ToString(),count);
}
// str[i].Remove(j,1); count = 0;
}
}
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(i.ToString()+":"+list.GetKey(i).ToString()+"-->"+list.GetByIndex(i).ToString());
}
/* IDictionaryEnumerator et = ht.GetEnumerator(); while (et.MoveNext())
{
Console.WriteLine(et.Key.ToString() + "->" + et.Value.ToString());
}*/
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
string time2 = DateTime.Now.ToString();
Console.WriteLine((DateTime.Parse(time1) - DateTime.Parse(time2)).ToString());
}
}
}
这个我以前写的
然后比较,就跟比较验证码一样的
是什么意思呀??
问题:键盘输入如“ABefgt”的字母串,使用C#编程实现计算每个字母出现次数的程序。
答:
string s=“ABefgt”;
int acont,bcount,ccount,dcount,...,zcount=0;
int len=s.length;s=s.replace("a",string.empty);
acount=len-s.length;
len=s.length;s=s.replace("b",string.empty);
bcount=len-s.length;
len=s.length;s=s.replace("c",string.empty);
ccount=len-s.length;
len=s.length;
.....
.....
.....s=s.replace("z",string.empty);
zcount=len-s.length;
len=s.length;
private void NewMethod()
{
List<string> list = new List<string>();//List string s = string.Empty;
if (textBox1.Text.Trim() != "")
{
char[] c = textBox1.Text.Trim().ToCharArray(); for (int i = 0; i < c.Length; i++)
{
int count = 0;
for (int j = 0; j < c.Length; j++)//相同的累加
{
if (c[i] == c[j])
count++;
} s = c[i] + "=" + count;
if (list.IndexOf(s) == -1)//如果存在就不用加
{
list.Add(s);
this.textBox2.Text += s + "\r\n";
}
}
}
}
结果
f=3
s=15
d=12
不建议使用本方法
List<string> list = new List<string>();//List
List<string>我没学习过,为什么不建议此方法呢?
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Countstring
{
class Program
{
static void Main(string[] args)
{
List<char> list = new List<char>();
Console.WriteLine("请输入您要查询的字符串:\n");
string s = Console.ReadLine();
foreach (char ss in s)
{
list.Add(ss);
} Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
int count=0;
for (int i = 0; i < list.Count;i++ )
{
//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
int frontifsame = 0;
for (int j = i - 1; j >= 0; j--)
{
if (list[j] == list[i])
{
frontifsame++;
break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
}
} if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
{
count = 1;
for (int j = i + 1; j < list.Count; j++)
{
if (list[j] == list[i])
{
count++;
}
} Console.WriteLine("{0} 在您输入的字符串中出现的次数为: {1}.\n", list[i], count);
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Countstring
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入您要查询的字符串:\n");
string s = Console.ReadLine();
Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
int count=0;
for (int i = 0; i < s.Length;i++ )
{
//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
int frontifsame = 0;
for (int j = i - 1; j >= 0; j--)
{
if (s[j] == s[i])
{
frontifsame++;
break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
}
} if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
{
count = 1;
for (int j = i + 1; j < s.Length; j++)
{
if (s[j] == s[i])
{
count++;
}
} Console.WriteLine("{0} 在您输入的字符串中出现的次数为: {1}.\n", s[i], count);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Countstring
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入您要查询的字符串:\n");
string s = Console.ReadLine();
Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
int count=0;
for (int i = 0; i < s.Length;i++ )
{
if (s[i] < 65 || s[i] > 122||(s[i]>=90&&s[i]<97))//判断要统计的字符是否是英文字母,如果不是,就跳过不统计
{
break;
}
//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
int frontifsame = 0;
for (int j = i - 1; j >= 0; j--)
{
if (s[j] == s[i])
{
frontifsame++;
break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
}
} if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
{
count = 1;
for (int j = i + 1; j < s.Length; j++)
{
if (s[j] == s[i])
{
count++;
}
} Console.WriteLine("{0} 在您输入的字符串中出现的次数为: {1}.\n", s[i], count);
}
}
}
}
}
using System.Linq;namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string str = "asdoisjdfoiajsdfoijasdjssdfsdf";
var result = from item in str group item by item into groups select groups; foreach (var charResult in result)
Console.WriteLine("{0}, {1}", charResult.Key, charResult.Count());
}
}
}
都不很好 用sortliststatic void Main(string[] args)
{
string data = "asd646uio4asda .sdjhglajsdoajam] 4uio64aaaksd;ka;s";
SortedList<char, int> charCount = new SortedList<char, int>();
foreach (char i in data)
{
if (!charCount.ContainsKey(i))
{
charCount.Add(i, 1);
}
else
{
charCount[i]=charCount[i]+1;
}
} foreach (KeyValuePair<char, int> i in charCount)
{
Console.WriteLine(" "+i.Key+": "+i.Value);
}
}保证可以用 还是排序好了的
我也试过,虽然以前一直不太相信。可以用个10K左右的字符串试试!想排序也很容易var result = from item in str group item by item into groups orderby groups.Count() descending select groups;按照字符重复次数排序从高到低var result = from item in str group item by item into groups orderby groups.Key select groups;按照字符排序
http://download.csdn.net/source/1842593】
linq对于初学者还是有点难理解的 相对sortlist要难点 sortlist跟hash'表差不多 容易理解
System.out.print("请输入你字符串");
String text=input.nextLine();
Map map=new HashMap(); for(int i=0;i<text.length();i++){ String key=text.charAt(i)+""; if(map.containsKey(key)){
int val=Integer.valueOf(map.get(key)+"")+1;
map.put(key,val);
}else{
map.put(key, 1);
}
} Iterator key=map.entrySet().iterator(); while(key.hasNext()){
Map.Entry entry=(Map.Entry)key.next();
Object keys=entry.getKey();
Object value=entry.getValue();
System.out.println(keys+"的个数为"+value);
}
}
class Program
{
public static int numWord(string word, string[] wordList)//查询字母在字符数组中出现的次数查询
{
int cout = 0;
for (int i = 0; i < wordList.Length; i++)
{
if (word.Equals(wordList[i], StringComparison.Ordinal))
cout++;
}
return cout;
}
static void Main(string[] args)
{
int cout ; //定义字母出现的查询
string word; //要查询的字母
string strWordlist;//要输入的字符串
string[] wordList = new string[100];
Console.WriteLine("请输入字符串数组");
strWordlist = Console.ReadLine();
for (int i = 0; i < strWordlist.Length; i++) //讲输入的字符串放入字符数组中
{
wordList[i] = strWordlist.Substring(i, 1);
}
Console.WriteLine("请输入你要查询的字母");
word = Console.ReadLine();
cout=numWord(word,wordList);
Console.WriteLine(cout);
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
string a = "asdfeDSds";
for (int i = 'A'; i <= 'Z'; i++)
{
Console.WriteLine(((char)i).ToString()+":"+(a.Length-a.Replace(((char)i).ToString(),"").Length));
}
for (int i = 'a'; i <= 'z'; i++)
{
Console.WriteLine(((char)i).ToString() + ":" + (a.Length - a.Replace(((char)i).ToString(), "").Length));
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication17
{
class Program
{
static int a = 0;
static void Main(string[] args)
{
Console.WriteLine(sumA());
}
static int sumA()
{
char[] text = Console.ReadLine().ToString().ToCharArray();
for (int i = 0; i < text.Length; i++)
{
if (text[i] == 'a')
a++; }
return a;
}
}
}
from ch in strTest
group ch by ch into gr select gr; foreach (var groupText in query)
Console.Write( (char)groupText.Key); Console.WriteLine();
foreach (var groupText in query)
{
Console.Write( groupText.Count());
}
Console.ReadLine();