How can I find the number of words in a given string?
问题 I'm trying to find the number of words in a given string in Pascal? This is my starter coede: 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. 回答1: You may implement the program as finite-state machine with two states ("inside word" and "word separator"): Program P1; type TState = (INSIDE_WORD, WORD_SEPARATOR); var s:string; i,k:integer; state: TState;