压缩字符串的函数

需求:

给定指定长度的字符串(由字母构成),要求输出没有重复的字母串,重复的字母要求显示出现的次数。

实现:

Demo

 

<span style="font-family:KaiTi_GB2312;font-size:18px;">/************************************************************************************
 *机器名称:zlt
 *作者:周丽同
 *小组:无
 *修改时间:2016年8月19日
/************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace strl
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "aaabbbcccdefg";//获取一个字符串
            char[] chars = str.ToCharArray();//将字符串类型转为字符串数组类型
            List<char> list1 = new List<char>();//实例化一个list

            for (int i = 0; i < chars.Length; i++)//遍历循环字符串数组中每一个字符
            {
                int w = 0;
                for (int j=0; j < chars.Length; j++)//循环判断是否有重复的字符
                {
                    if (chars[i] == chars[j])//如果有类似的字符,计数加一
                    {
                        w = w + 1;
                    }
                }
                if(w>1)//判断有重复的字符
                {
                    int b = 0;
                    for (int d = 0; d < list1.Count; d++)
                    {
                        if (chars[i] == list1[d])
                        {
                            b = b + 1;
                        }
                    }
                    if (b == 0)
                    {
                        Console.Write(w + "" + chars[i]);
                    }
                    list1.Add(chars[i]);
                }
                else//如果没有重复的字符
                {
                    Console.Write(chars[i]);//输出该字符
                }
            }
            Console.ReadKey();
        }
        
    }
}</span>

效果:


转载于:https://www.cnblogs.com/zhoulitong/p/6412354.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值