帮助 Pascal 编写字计数器
我必须用 Pascal 编写一个程序,它必须检测文本(由用户输入)中有多少单词以某个字母开头。我无法使用数组,您能给我一些关于从哪里开始的提示吗?
I have to write a program in Pascal which has to detect how many words on a text (input by the user) start with a certain letter. I can't use arrays, can you give me any hints as to where to start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你知道哪个字母,你只需要保留一个计数器,不需要数组。
如果你不知道哪个字母,就保留 26 个计数器。愚蠢,但可以按照您的规范工作。
If you know which letter, you merely need to keep a counter, no need for arrays.
If you don't know which letter, keep 26 counters. Stupid, but works as per your spec.
首先要做的是定义构成字母的字符集,或者反过来定义哪些字符构成非字母。
编写一个函数,该函数接受一个字符并根据该字符是否是字母返回一个布尔值。然后循环遍历字符串并为每个字符调用它。当您检测到非字母后面或字符串开头的字母时,如果它是目标字母,则增加计数器。
First thing to do is define the set of characters that constitute letters, or conversely which ones constitute non-letters.
Write a function that takes a character and returns a boolean based on whether that character is a letter. Then loop through the string and call it for each character. When you detect a letter right after a non-letter or at the start of the string, increment your counter if it is the target letter.
计算 SPACE LETTER 的实例以及第一个单词(如果匹配)。
count instances of SPACE LETTER plus first word if it matches.
(S) 是您的输入字符串;
注意循环大小的负一。
另外,请记住,字符串的第一个字母可能是您想要匹配的字母,并且上面定义的循环不会拾取该字母。
如果您需要使代码更智能,因为它可以定位特定字母而不是硬编码的“t”,那么您可以将请求的字符作为参数传递给循环所在的函数/过程。
(S) is your input string;
Note the minus one on the loop size.
Also, remember that the very first letter of the string may be the one you want to match and that will not get picked up by the loop defined above.
If you need to make your code smarter in that it can locate a specific letter rather than a hardcoded 't' then you can pass the requested character as a parameter to the function/procedure that your loop is in.
超出我的想象 - 未测试
为什么你需要一个数组或一个 case 语句?
Off the top of my head - not tested
Why would you need an array or a case statement?