Skip to content

Instantly share code, notes, and snippets.

@rahulkhatri19
Created February 18, 2020 04:44
Show Gist options
  • Save rahulkhatri19/8d1c01acd27f4ce95df50cef7b8d4cf4 to your computer and use it in GitHub Desktop.
Save rahulkhatri19/8d1c01acd27f4ce95df50cef7b8d4cf4 to your computer and use it in GitHub Desktop.
For counting vowel in a string by Recursive method.
var count = 0
fun main(args: Array<String>) {
println("Enter string to Count Vowels: ")
val inputSt: String? = readLine()!!
// val inputSt: String? = "Hi this to check"
print(checkforVowel(inputSt, inputSt?.length))
}
fun checkforVowel(inputSt: String?, length: Int?): Int {
if (length == 1){
return isVowel(inputSt?.toCharArray()?.get(length.minus(1)))
}
else return checkforVowel(inputSt, length!!.minus(1)) + isVowel(inputSt?.toCharArray()?.get(length.minus(1)))
}
fun isVowel(ch:Char?): Int {
val checkVowel = ch?.toUpperCase()
if (checkVowel == 'A' || checkVowel == 'E' || checkVowel == 'I' || checkVowel == 'O' || checkVowel == 'U'){
// count += 1
return 1
} else return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment