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

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

时间:2017-04-14 12:42:44      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:har   sys   content   class   return   ios   bsp   space   pac   

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#中操作就很easy了,无论是数组操作,还是泛型LIST都能够解决。 


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

标签:har   sys   content   class   return   ios   bsp   space   pac   

原文地址:http://www.cnblogs.com/liguangsunls/p/6708182.html

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