计算字符串中的元音 [英] count the vowels in a string

查看:156
本文介绍了计算字符串中的元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午,我已经找到了一种方法来计算字符串中的元音并且已经接近了,

Afternoon, I have searched for a way to count the vowels in a string and have got close,

Sub countVowels(userInput As String, ByRef numberOfVowels As Double)

        Dim userInputArray() As Char
        Dim mycount As Integer
        Dim s As String

        userInputArray = userInput.ToCharArray

        System.Console.WriteLine("  THE USER ENTERED : [ " & userInput & " ] ")
        ' COMMENT : user the user input array variable to count the number of vowels

        numberOfVowels = 0
        If 
            (userInputArray(mycount) = "A") Or
            (userInputArray(mycount) = "E") Or
            (userInputArray(mycount) = "I") Or
            (userInputArray(mycount) = "O") Or
            (userInputArray(mycount) = "U") Or
            (userInputArray(mycount) = "a") Or
            (userInputArray(mycount) = "e") Or
            (userInputArray(mycount) = "i") Or
            (userInputArray(mycount) = "o") Or
            (userInputArray(mycount) = "u") Then




            numberOfVowels = numberOfVowels + 1

        End If

            Console.WriteLine("number of vowels:" & numberOfVowels)

    End Sub




推荐答案

你正在接近,但你需要一个循环来完成每一个轮流中的角色:目前你只看第一个(并且运气好,因为你没有给 mycount 赋值。)

我还会使用选择案例而不是长如果条件:

You're getting close, but you need a loop to go through each of the characters in turn: at the moment you only look at the first one (and that by luck, since you don't ever assign a value to mycount).
I'd also use a Select Case instead of long If condition:
Dim vowels As Integer = 0
For Each c As Char In userInput
	Select Case c.ToLower()
		Case "a"C, "e"C, "i"C, "o"C, "u"C
			vowels += 1
			Exit Select
	End Select
Next


这段代码就是我所谓的反编程;而不是使用编程抽象,它反映了非工程师的想法,他们试图逐一比较和计算所有东西。在真正的编程中,代码总是更具表现力和更短。此外,它应该是可维护的:你应该避免隐式定义的所有立即常量并分散在某些代码文本范围内。最好明确定义所有常量并将它们组合在一起。



所以,这里有一个简单的算法:



首先,定义元音。通常,没有预定义的函数来告诉所有书写系统和语言中的辅音元音,但正如我所看到的,你只对英语感兴趣。正确的编程风格将帮助您将算法本地化到不同的文化。因此,将元音集定义为只是一个常量字符串。不要放小写和大写字母。而是以小写或大写形式进行所有计算。我们假设,对于下面的所有考虑,您希望使用小写。所以,定义了像aeiouy这样的字符串。



写一个单独的函数NumberOfVowels(string)。始终将代码的所有声音功能单元抽象为函数。



在此函数中,首先计算输入字符串的小写表示。这将是一个电话。或者,在循环中按字符执行。



现在,循环。在循环之前,声明整数变量 count 并将其指定为0.遍历输入值中的所有字符。对于每个字符,在常量元音字符串中搜索此字符(aeiouy)。如果搜索成功(忽略找到的索引;您只需要使用常量字符串的长度),请增加 count 。搜索也只是对库函数的一次调用。



循环后,返回 count



这就是全部。



现在,您甚至没有指定Basic是什么语言,确切地说,但 System.Console.WriteLine 表明它是VB.NET。然后小写函数可以是 System.String.ToLower()(或者你可以使用 System.String.ToUpper()),搜索功能可以是 System.String.IndexOf(System.Char)

https://msdn.microsoft.com/en-us/library/system.string%28v = vs.110%29.aspx [ ^ ]。



使用函数 System.String.IndexOf(System。字符串),未找到条件将由返回值小于零指示。







在对这个答案的评论中,0x01AA提出了一个很好的选择,LINQ。我喜欢它,但我觉得很难为你解释,因为我不知道你的背景;对于初学者来说,理解LINQ可能相当困难。也许0x01AA会尝试在他的替代答案中做到这一点。







As Bruno(0x01AA)决定不回复(见下面的评论),我可以解释如何获得LINQ解决方案,而无需编写任何代码。



LINQ示例可以在这里找到: https://msdn.microsoft.com/en-us/library/bb397940.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 [ ^ ]。



如何将其更改为您需要的?简单。替换行

This code is what I call "anti-programming"; instead of using programming abstraction, it reflects the thinking of a non-engineer who tries to compare and count everything one by one. In "real" programming, the code is always much more expressive and shorter. Besides, it should be maintainable: you should avoid all your immediate constants defined implicitly and disperse over some range of code text. Better define all constants explicitly and group them together.

So, here is the simple algorithm for you:

First, define vowels. Usually, there is no predefined functions to tell vowels from consonants in all writing systems and languages, but as I can see, you are only interested in English. Correct programming style will help you to localize the algorithm to different culture. So, define the set of vowels as just one constant string. Don't put both lower-case and upper-case letters. Instead, do all calculations in either lower case or upper case. Let's assume, for all considerations below, you want to use lower-case. So, defined the string like "aeiouy".

Write a separate function NumberOfVowels(string). Always abstract out all sound functional units of your code into functions.

In this function, first calculate lower-case representation of your input string. It would be one call. Alternatively, do it per character inside the loop.

Now, the loop. Before the loop, declare the integer variable count and assign it to 0. Traverse all characters in the input value. For each character, perform the search of this character in the constant string of vowels ("aeiouy"). If the search is successful (ignore the index found; you only need to have it withing the constant string's length), increment count. The search is also only one call to the library function.

After the loop, return count.

That's all.

Now, you did not even specify what language do you mean by "Basic", exactly, but System.Console.WriteLine suggests it's VB.NET. Then the lower-case function could be System.String.ToLower() (or you can use System.String.ToUpper()), and the search function can be System.String.IndexOf(System.Char):
https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^].

With the function System.String.IndexOf(System.String), "not found" condition will be indicated by return value less then zero.



In comments to this answer, 0x01AA suggested a good alternative, LINQ. I like it, but I would find it quite difficult to explain it for you, because I don't know your background; understanding of LINQ could be pretty hard for a beginner. Perhaps 0x01AA will try to do it in his alternative answer.



As Bruno (0x01AA) decided not to post an answer (see the comments below), I can explain how to get the LINQ solution, without writing any code.

The LINQ sample can be found here: https://msdn.microsoft.com/en-us/library/bb397940.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1[^].

How to change it to what you need? Simple. Replace the line
Where Char.IsDigit(ch)



with


with

Where vowels.Contains(ch.ToLower())



在此line,元音应该是你正在搜索的常量元音串(等于aeiouy)。



很抱歉,但我无法解释LINQ的工作原理,涉及的类型等等。我真的不知道你的背景;我猜你需要学习很多基础知识才能真正理解狡猾的LINQ技术。请参阅:

https://en.wikipedia.org/wiki/Linq [< a href =https://en.wikipedia.org/wiki/Linqtarget =_ blanktitle =New Window> ^ ],

https://msdn.microsoft.com/en-us/library/bb397926.aspx [ ^ ]。



-SA


In this line, vowels should be your constant string of vowels you are searching in (equals to "aeiouy").

Sorry, but I cannot take the labor of explaining how LINQ works, what types are involved, and so on. I don't really know your background; and I guess you would need to learn a lot of fundamentals before you can really understand cunning LINQ techniques. Please see:
https://en.wikipedia.org/wiki/Linq[^],
https://msdn.microsoft.com/en-us/library/bb397926.aspx[^].

—SA


这篇关于计算字符串中的元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆