• C#l练习:①统计特定字符段出现的次数②统计所有字符出现的次数


    ①统计特定字符段出现的次数

    代码实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {    
       
        class Program
        {
            static void Main(string[] args)
            {
                int sum = 0;
                string s1 = "患者:“大夫,我咳嗽得很重。”     大夫:“你多大年记?”     患者:“七十五岁。”     大夫:“二十岁咳嗽吗”患者:“不咳嗽。”     大夫:“四十岁时咳嗽吗?”     患者:“也不咳嗽。”     大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";
                char[] a = s1.ToCharArray();
                for (int i = 0; i < a.Length - 1; i++)
                {
                    if (a[i] == '咳' && a[i + 1] == '嗽')
                        sum++;
    
                }
    
                Console.WriteLine("咳嗽字符串出现的次数为{0}", sum);
                Console.ReadKey();
           
    
    
                     
    
    
    
    
    
    
            }
        }
    }
    

    输出结果:

    ②统计所有字符出现的次数

    代码实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {    
       
        class Program
        {
            static void Main(string[] args)
            { 
                string s1 = "患者:“大夫,我咳嗽得很重。”     大夫:“你多大年记?”     患者:“七十五岁。”     大夫:“二十岁咳嗽吗”患者:“不咳嗽。”     大夫:“四十岁时咳嗽吗?”     患者:“也不咳嗽。”     大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";
                char[] a = s1.ToCharArray();
                int[] a1=new int [a.Length];//计数数组
                int[] a2=new int [a.Length];//标记数组
                char c4;
                for (int i = 0; i < a.Length; i++)
                {
                    a1[i] = 0;
                    a2[i] = 0;
                }
                for (int i = 0; i < a.Length; i++)
                {
                    if (a2[i] == 0)//为遍历过就将他查询
                    {
                        c4 = a[i];//获取值
                        for (int j = i; j < a.Length; j++)
                        {
                            if (a[j] == c4 && a2[j] == 0)//查询他的值之后的
                            {
                                a1[i]++;//统计与他相同的值
                                a2[j] = 1;//标记已经被计算过
                            }
    
    
    
                        }
                    }
                }
                for (int i = 0; i < a.Length; i++)
                {
                    if (a1[i] > 1)
                    {
                        Console.Write("{0}    ", a[i]);
                        Console.Write("{0}   ", a1[i]);
                    }
                }
                Console.ReadKey();
            }
        }
    }
    

      实现结果:

  • 相关阅读:
    9、实战快速上手
    8、路由【前端实现页面的跳转】
    7、Webpack的学习【打包工具】
    6、vue的安装【nodejs、vue-cli】
    5、计算属性、内容分发、自定义事件
    4、Axios异步通信
    3、Vue表单的双向绑定以及第一个Vue组件
    2、Vue的基本属性
    PHP算法之寻找两个有序数组的中位数
    PHP算法之无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/zykh/p/7711465.html
Copyright © 2020-2023  润新知