码迷,mamicode.com
首页 > 编程语言 > 详细

字符数组在C++、C#等语言中的操作

时间:2014-05-05 13:29:30      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:c#   c++   

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都可以解决。 


字符数组在C++、C#等语言中的操作,布布扣,bubuko.com

字符数组在C++、C#等语言中的操作

标签:c#   c++   

原文地址:http://blog.csdn.net/yancongmin0702/article/details/24875267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!