zoukankan      html  css  js  c++  java
  • 题目1021:统计字符------------------------主要注意输入的方法,如何让一长串的字符都接收到字符串中

    关于接收字符,我用的是输入到‘’就结束的策略;

    #include<stdio.h> 
    int main()
    { 
        while(true)//这里用的while(true)来表示无限循环
        {    
        int i=0,j=0;
            int count[5]={0,0,0,0,0};
            char str1[5],str2[80]; 
           char ch;
            while(scanf("%c",&ch)!=EOF)
            {
                if (ch=='#') return 0;
                else if (ch=='
    ') break;
                else  str1[i++]=ch; 
            }
            str1[i]='';
            char cr;
             while(scanf("%c",&cr)!=EOF) 
             {
                 if(cr=='
    ') break;
                   else str2[j++]=cr;
             }
             str2[j]='';
         for(i=0;str1[i]!='';i++)
            for(j=0;str2[j]!='';j++) 
              if (str2[j]==str1[i]) count[i]++;
         
        for (i=0;i<5;i++) 
            if (count[i]!=0)
              printf("%c %d
    ",str1[i],count[i]);
        }  
         return 0;
    } 

    其实,我最中意的是这种代码:转载自http://blog.csdn.net/mnmlist/article/details/25185563

     #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        while(true)//同样的无限循环的策略
        {
            int count[1000]={0};
            string str1,str2;
            getline(cin,str1);//标准的c++式的输入一行,当然空格也接收
            if(str1=="#")
                break;
            getline(cin,str2);
            int k=str1.length();
            for(int i=0;i<k;i++)
            {
                int j=0;
                while(true)
                {
                    j=str2.find(str1[i],j);//用的是关于string 的标准容器的find函数
                    if(j!=-1){count[i]++;j++;}
                    else
                        break;
                }
                cout<<str1[i]<<' '<<count[i]<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    .net 中文显示乱码问题(Chinese display with messy code)
    Compare the value of entity field.
    人见人爱A^B 题解
    人见人爱A-B 题解
    全局变量
    第39级台阶 题解
    马虎的算式 题解
    做题技巧
    inline用法
    queue函数用法
  • 原文地址:https://www.cnblogs.com/jianrenguo/p/6508956.html
Copyright © 2011-2022 走看看