There is a task:

A text (an array of character strings) is given, each character of which can be a letter, a digit, or one of the +, -, * signs. Count the number of occurrences of the letter f in the first three groups of letters.

Solve need on pascal.

With numbers it is still clear. But with the characters. I won’t figure out how to fill the array at random so that there are letters, numbers, and +, -, * signs. In this case, the parameters of the array need to be determined from the keyboard. All this should be in two procedures. One to enter the matrix. The second to display the matrix on the screen. Or is there no such possibility in Pascal? Then it is better to enter lines from the keyboard?

Then count the first three groups of letters. This means that the line can be, for example, five words separated by spaces, and the search for the letter f should be performed only in the first three words. I understood correctly?

How can this be implemented?

Please show me an example, but since yesterday, brains with this task are already on the back :)

    3 answers 3

    You can generate an array consisting of random strings in different ways: set the alphabet from which each string will be composed or select a random character by code from the encoding. I used the first method.

    Const N0=100; (* число элСмСнтов массива ΠΈ массив-Π°Π»Ρ„Π°Π²ΠΈΡ‚ *) Alf: Array[0..38] Of Char = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '+', '-', '*', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); Var StrMas: Array[0..N0-1] Of String; i, j, n, l: Byte; AlfInd: 0..38; begin Repeat (* ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΎΡ‡ΠΊΠ° Π²Π²ΠΎΠ΄Π° числа элСмСнтов *) Write('N = '); ReadLn(N); Until (N In [1..99]); Dec(N); (* Π²Ρ‹Ρ‡ΠΈΡ‚Π°ΡŽ 1 (N:=N-1; - Ρ‚ΠΎ ΠΆΠ΅ самоС) *) Randomize; For i:=0 To N Do (* ΠΌΠΎΡ‚Π°ΡŽ Ρ†ΠΈΠΊΠ» ΠΎΡ‚ 0 Π΄ΠΎ число элСмСнтов - 1 *) Begin StrMas[i]:=''; (* элСмСнт сначала Ρ€Π°Π²Π΅Π½ пустому символу *) l:=Random(50)+1; (* случайная Π΄Π»ΠΈΠ½Π° строки, Π½ΠΎ Π½Π΅ мСньшС 1 *) For j:=1 To L Do (* ΠΌΠΎΡ‚Π°ΡŽ Ρ†ΠΈΠΊΠ» ΠΎΡ‚ 1 Π΄ΠΎ Π΄Π»ΠΈΠ½Ρ‹ Π±ΡƒΠ΄ΡƒΡ‰Π΅ΠΉ строки *) StrMas[i]:=StrMas[i]+Alf[Random(38)]; (* заполняю случайными элСмСнтами *) End; (* Π²Ρ‹Π²ΠΎΠΆΡƒ массив *) For i:=0 To N Do WriteLn(StrMas[i]); WriteLn('Davi na ENTER!!!'); ReadLn; end. 

    And the second part of the task requires additional explanations: the task does not indicate that there can be spaces in the lines, so a group of letters is most likely something else: an element of an array, or a certain number of characters (known in advance), or something else.

      Unclear. "Dan text ..." - it is entered from the keyboard or generated randomly?

      By the way, it is very simple to generate - we create a line like "abcdefghij ... yz0123456789 + - *" (there are several spaces at the end), generate a random number from 0 to the number of characters in this line, remove the character with this random number and add it to the result string. To increase the likelihood that we will have several groups of words, make some spaces)

      What parameters should be determined from the keyboard (how is it in pascal ... readln?).

      I will not write the code, I have not touched pascal for a long time. But the most primitive algorithm may well be like this:

      We iterate over the characters in the given line from the first to the end, compare each character with a space and the letter f, each space and a letter, of course, is assumed. If there is a third space, then exit the loop. In general, that's all.

        Well, you can just fill the array

         Var mas:array['-','+','*','a','b'....,'0']; // синтаксис Π΅ΠΉ Π±ΠΎΠ³Ρƒ Π½Π΅ помню, Π΄Π°Π²Π½ΠΎ Π±Ρ‹Π»ΠΎ. Begin randomize; // Π½Ρƒ Ρ‚ΡƒΡ‚ ΠΊΠΎΠ³Π΄Π° заполняСшь строки просто впиши for i=:0 to len(mas) do begin rnd:=random(2)// ΠΎΡ‚ 0 Π΄ΠΎ 2 stroka[i]:=mas[rnd]; // Π΄ΠΎΠ±Π°Π²ΠΈΠΌ Π² строку символ ΠΏΠΎ Ρ€Π°Π½Π΄ΠΎΠΌΡƒ. end; // Π½Ρƒ ΠΈ Ρ‚Ρ‹ ΠΈΡ… Ρ‡Π΅Ρ€Π΅Π΄ΡƒΠΉ ΠΈΠ»ΠΈ сдСлай ΠΎΠ΄ΠΈΠ½ массив с числами Π±ΡƒΠΊΠ²Π°ΠΌΠΈ ΠΈ этими символами ΠΈ юзай ΠΎΠ΄ΠΈΠ½ массив. End. 

        Then just check in the loop those conditions that are needed.