如何找到给定字符串中的单词数? [英] How can I find the number of words in a given string?

查看:52
本文介绍了如何找到给定字符串中的单词数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Pascal中查找给定字符串中的单词数?

I'm trying to find the number of words in a given string in Pascal?

这是我的入门杂文:

Program P1;
 var s:string;
     i,k:integer;
 begin
   write('Enter a string:  '); readln(s);
   k:=0;
   for i:=1 to length(s) do
    begin
        if(s[i] = ' ') then k:=k+1;
    end;
    write('Number of words ', k);
end.

推荐答案

在免费Pascal中,有一个 wordcount 函数:

In Free Pascal there is a wordcount function in the strutils unit:

uses strutils;
var s : string;
begin 
  write('Enter a string:  '); readln(s);
  writeln('Number of words: ',wordcount(s,[' ','.',',']));
end;

这篇关于如何找到给定字符串中的单词数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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