1,C++中操作数组
#include <iostream>
using namespace std;
int length(char []);
void output_frequency(char []);
int main()
{
char str[]="yan cong min";
cout<<"要处理的字符串为:"<<str<<endl;
cout<<"字符串长度为:"<<length(str)<<endl;
cout<<"字符串中各字符出现的频数为:";
output_frequency(str);
cout<<endl;
system ("Pause");
}
int length(char s[])
{
int i=0;
while(s[i]!='\0') i++;
return i;
}
void output_frequency(char str[])
{
int i , j ,num;
for(i=0;i <= length(str);i++)//length(str)在for之前求出来保存到一个变量中就更好了
{
num = 0;
for(j=0;j<length(str);j++)
{
if(str[i]==str[j])
{
if(i>j)
break; //在i之前发现了等待统计的字符,break了之
else
num++; //这时已经过了大限,++即可
}
}
if(num!=0) //这儿我做些改动,只考虑不等于0即可
cout <<str[i]<<"-"<<num<<"; ";
}
}
2,在C#中操作就非常简单了,不管是数组操作,还是泛型LIST都可以解决。