kotlin 字符串去空格_Kotlin程序查找字符串中的元音,辅音,数字和空格的总数

kotlin 字符串去空格

Given a string, we have to count the total number of vowels, consonants, digits and spaces in a string.

给定一个字符串,我们必须计算字符串中元音,辅音,数字和空格的总数。

Example:

例:

    Input:
    string = "Heloo IncludeHelp this is your code 123#$567"

    Output:
    Vowels : 13
    Consonants : 17
    Digits : 6
    white Spaces : 6

该程序在Kotlin中查找字符串中的元音,辅音,数字和空格的总数 (Program to find total number of vowels, consonants, digits and spaces in a string in Kotlin)

package com.includehelp.basic

import java.util.*

//Main function, Entry Point of Program
fun main(args: Array<String>) {
    //Input Stream
    val sc = Scanner(System.`in`)

    var vowels=0
    var consonants=0
    var whitespace=0
    var digits=0
    
    
    //Input String Value
    println("Enter String : ")
    val sentence = sc.nextLine()

    //check for Null or Empty String
    if(!sentence.isNullOrEmpty()){
        //Convert String to lower case
        val s=sentence.toLowerCase()

        //iterate loop to count vowels, consonants, digits and spaces
        for(i in s.indices){
           when(s[i]){
               'a','e' ,'i' ,'o' ,'u' -> vowels++
               in 'a'..'z' -> consonants++
               in '0'..'9' -> digits++
               ' ' -> whitespace++
           }
        }

        println("Vowels : $vowels")
        println("Consonants : $consonants")
        println("Digits : $digits")
        println("white Spaces : $whitespace")
    }
    else{
        println("Sentence is Null or Empty")
    }
}

Output

输出量

Run 1:
Heloo IncludeHelp this is your code 123#$567
Vowels : 13
Consonants : 17
Digits : 6
white Spaces : 6
---
Run 2:
Delhi Corona Virus Death Count is 567
Vowels : 12
Consonants : 16
Digits : 3
white Spaces : 6


翻译自: https://www.includehelp.com/kotlin/find-total-number-of-vowels-consonants-digits-and-spaces-in-a-string.aspx

kotlin 字符串去空格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值