Программа 3_04.bas
RЕМ Подсчет числа слов в строке
CLS
SPACE=1: WORDS=0
PRINT "Введите строку"
INPUT "",S$
LENS=LEN(S$)
IF LENS=0 THEN GOTO RET
IF LEFT$(S$,1)<>" " THEN SPACE=0: WORDS=1
FOR J=2 TO LENS
IF SPACE=1 AND MID$(S$,J,1)<>" " THEN SPACE=0: WORDS=WORDS+1
IF SPACE=0 AND MID$(S$,J,1)=" " THEN SPACE=1
NEXT J
RET:
PRINT "Число слов в строке = "; WORDS
END
Программа 3_04.с
/* Подсчет числа слов в строке */
#include <stdio.h>
#include <conio.h>
#include <string.h>
main() {
char s[81], space=l, words=0, i,len;
clrscr ();
puts("Введите строку");
gets (s) ;
len=strlen(s);
if (len=0) goto ret;
if (s[0]!=' ') {
space=0;
words=l; }
for(i=l; i<len; i++) (
if (space==l &&, s[i]! = ' ') {
space=0; words++; }
if (space==0 && s[i]==' ')space=l; } ret:
printf("\n Число слов в строке = %d",words);
getch () ; }
Программа 3_04.pas
program num_words;
{ Подсчет числа слов в строке }
label ret;
var
s:string[81];
space:boolean;
words:byte;
i,len:byte; begin
writeln('Введите строку');
readln(s);
len:=length(s);
if len=0 then goto ret;
space:=(s[1]=' ');
if not space then words:=1;
for i:=2 to len do
begin
if (not space)and(s[i]=' ') then space:=true;
if (space)and(s[i]<>' ') then
Degin
space;=false; inc(words);
end;
end;
ret:
writeln('Число слов в строке = ',words);
readln;
end.
Задание 3.05. Анализ нажатой клавиши
Составить программу, которая анализирует код нажатой клавиши и выводит его на экран. Программа должна завершать свою работу после нажатия клавиши <Esc> (код клавиши равен 27).
Совет 1 (общий)