帮助 Pascal 编写字计数器

发布于 2024-08-07 03:02:54 字数 80 浏览 6 评论 0原文

我必须用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月无声 2024-08-14 03:02:54

如果你知道哪个字母,你只需要保留一个计数器,不需要数组。

如果你不知道哪个字母,就保留 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.

如日中天 2024-08-14 03:02:54

首先要做的是定义构成字母的字符集,或者反过来定义哪些字符构成非字母。

编写一个函数,该函数接受一个字符并根据该字符是否是字母返回一个布尔值。然后循环遍历字符串并为每个字符调用它。当您检测到非字母后面或字符串开头的字母时,如果它是目标字母,则增加计数器。

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.

弄潮 2024-08-14 03:02:54

计算 SPACE LETTER 的实例以及第一个单词(如果匹配)。

count instances of SPACE LETTER plus first word if it matches.

暮年慕年 2024-08-14 03:02:54

(S) 是您的输入字符串;

  1. 创建一个从 1 到 (S) - 1 长度的 for 循环。
  2. 在循环内部,检查 (S)[i] = ' ' 和 (S)[i+1] = 't',其中 i 是循环计数器,“t”是要计数的单词的开头字母。
  3. 如果第二步中的条件匹配,则递增计数器。

注意循环大小的负一。

另外,请记住,字符串的第一个字母可能是您想要匹配的字母,并且上面定义的循环不会拾取该字母。

如果您需要使代码更智能,因为它可以定位特定字母而不是硬编码的“t”,那么您可以将请求的字符作为参数传递给循环所在的函数/过程。

(S) is your input string;

  1. Create a for loop that goes from 1 to the length of (S) - 1.
  2. Inside loop, check is (S)[i] = ' ' and (S)[i+1] = 't' where i is the loop counter and 't' is the letter starting the word you want to count
  3. If criteria in step two matches then increment a counter.

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.

骄傲 2024-08-14 03:02:54

超出我的想象 - 未测试

function WordCount(const S: string; const C: Char): Integer;
const
  ValidChars: Set of Char [A..Z, a..z]; // Alter for appropriate language
var
  i : Integer;
  t : string;
begin
  Result := 0;
  if Length(S) <> 0 then
  begin
    t := Trim(S); // lose and leading and trailing spaces
    t := t + ' '; // make sure a space is the last char
    repeat
      if (t[1] in ValidChars) and (t[1] = C then
        inc(Result);
      i := Pos(' ', t);
      t := Copy(t(i+1, Length(t));
    until Length(t) = 0;
  end;
end;

为什么你需要一个数组或一个 case 语句?

Off the top of my head - not tested

function WordCount(const S: string; const C: Char): Integer;
const
  ValidChars: Set of Char [A..Z, a..z]; // Alter for appropriate language
var
  i : Integer;
  t : string;
begin
  Result := 0;
  if Length(S) <> 0 then
  begin
    t := Trim(S); // lose and leading and trailing spaces
    t := t + ' '; // make sure a space is the last char
    repeat
      if (t[1] in ValidChars) and (t[1] = C then
        inc(Result);
      i := Pos(' ', t);
      t := Copy(t(i+1, Length(t));
    until Length(t) = 0;
  end;
end;

Why would you need an array or a case statement?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文