博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin中判断字符串_Kotlin程序计算字符串中每个字符的出现
阅读量:2533 次
发布时间:2019-05-11

本文共 1683 字,大约阅读时间需要 5 分钟。

kotlin中判断字符串

Given a string, we have to count the occurrences of each character.

给定一个字符串,我们必须计算每个字符的出现次数。

Example:

例:

Input:    string = "Hello includehelp how are you !!"    Output:    { =5, a=1, !=2, c=1, d=1, e=4, H=1, h=2, i=1, l=4, n=1, o=3, p=1, r=1, u=2, w=1, y=1}

程序,用于计算Kotlin中字符串中每个字符的出现 (Program to count the occurrences of each character in a string in Kotlin)

In this program, we are using the concept of HashMap, which will contain the individual characters along with its occurrences in the string.

在此程序中,我们使用HashMap的概念,该概念将包含各个字符及其在字符串中的出现。

package com.includehelp.basicimport java.util.*//Main Function, entry Point of Programfun main(args: Array
) { //Input Stream val sc = Scanner(System.`in`) //input April20.string value println("Input String : ") val str: String = sc.nextLine() val characterHashMap: HashMap
= HashMap
() var c: Char // Scan string and build hash table for(i in str.indices){ c = str[i] if (characterHashMap.containsKey(c)) { // increment count corresponding to c characterHashMap[c] = characterHashMap[c]!!+1 } else { characterHashMap[c] = 1 } } //print All Occurrence of character println("All character Count: $characterHashMap")}

Output

输出量

Run 1:Input String :Hello includehelp how are you !!All character Count: { =5, a=1, !=2, c=1, d=1, e=4, H=1, h=2, i=1, l=4, n=1, o=3, p=1, r=1, u=2, w=1, y=1}---Run 2:Input String :Hindustan India android Kotlin Java FlutterAll character Count: { =5, a=5, d=4, e=1, F=1, H=1, i=4, I=1, J=1, K=1, l=2, n=5, o=2, r=2, s=1, t=4, u=2, v=1}

翻译自:

kotlin中判断字符串

转载地址:http://pkvzd.baihongyu.com/

你可能感兴趣的文章
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>