zoukankan      html  css  js  c++  java
  • 字符数组在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]!='') 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都能够解决。 


  • 相关阅读:
    JS4
    JS3
    JS2
    JS1
    Dos命令
    面向对象的复习
    9.14Css
    9.13列表的用法
    9.12Css
    9.11Css
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6708182.html
Copyright © 2011-2022 走看看