Character data type in Pascal presentation. Presentation on the topic Strings in Pascal




Syntax: var s: string[n]; var s1: string; n - maximum possible string length - an integer in the range. If this parameter is omitted, then by default it is set to 255.


String constants are written as sequences of characters delimited by apostrophes. S: = "Text string"


A blank character is indicated by two consecutive apostrophes. If an apostrophe is included in a line as a character, then it is doubled when written. Variables declared as strings with different maximum lengths can be assigned to each other, although if you try to assign a short variable to a long one, extra characters will be discarded. Expressions of type char can be assigned to any string variables.


Pascal provides easy access to the individual characters of a string variable: i-th character variable st is written as st[i]. For example, if st is "String", then st is "C", st is "t", st is "p", and so on.


A merging (concantenation) operation is defined on string data, denoted by the + sign. For example: a:= "Turbo"; b:= "Pascal"; c:= a + b; In this example, the variable c will become "TurboPascal".


Procedures and functions for working with strings 1)n:= length("Pascal"); (n will be equal to 6) 2) s:= "Turbo Pascal System"; s2:= copy(s, 1, 7); (s2 will be equal to "System") s3:= copy(s, 9, 5); (s3 will be equal to "Turbo") s4:= copy(s, 15, 6); (s4 will be equal to "Pascal")

Presentation on the topic "Strings in Pascal" in computer science in powerpoint format. This presentation for 10th grade schoolchildren discusses theoretical material and tasks on the topic of string data in the Pascal programming language. Author of the presentation: computer science teacher, Yudin A.B.

Fragments from the presentation

Theory

  • A string is an array of characters, i.e. elements of type char. In Pascal, a string has a String data type.
  • If the length is not specified, memory is allocated for a string of up to 255 characters.
  • Task 1. Create a program that counts the number of letters in a word.
  • Task 2. Compare the length of two lines entered from the keyboard.

Soru function

  • The Copy(S, P, N) function extracts a substring of N characters from the string S, starting at position P. Here N and P are integer expressions.
  • Task 3. Create a program that cuts letters from the word COMPUTER SCIENCE so that they make the word CAKE.
  • Task 4. Write a program that prints the first and last digit of a natural number entered from the keyboard.

VAL procedure

The val procedure allows you to convert digital characters (the image of a number) into a number. IN general view calling the procedure looks like this:

  • VAL (String, Number, Code) ;
  • where String is a string constant or variable containing an image of a number;
  • Number – a variable of integer or fractional type to which a value must be assigned
  • Code – error code returned by the procedure (integer)

Type Conversion

PascalABC has a more advanced arsenal of transformations like DELPHI:

  • S:=IntToStr(N) - converts an integer to a string;
  • n:=StrToInt(S) - converts a string to an integer
  • S:=FloatToStr(r) - converts a real number to a string
  • R:=StrToFloat(s) converts a string to a real number.

If the conversion is not possible, a run-time error occurs

ORD and CHR functions

  • Function Ord(S) – determines the ordinal number of the symbol.
  • Function Chr(i) – determines the character with serial number i

Task 6. Create a program that, based on a character entered from the keyboard, displays its number in the code table.

Counting characters in a line

Problem 7. Given a string of characters. Determine how many times the letter A (Russian) appears in it.

Replacing characters in a string

Problem 8. Given a string of characters. Replace all the letters A in it with the letter O.

Removing characters from a string

Task 9 Remove all letters R (Latin, capital) from the string entered from the keyboard.

Nested conditions

Task 10. In the given text, everywhere in the given text, replace the letter “a” with the letter “b”, and the letter “b” with the letter “a”.

Nested Loops

Problem 11. Given a string of characters containing punctuation marks. Count what and how many punctuation marks a line contains.


























Enable Effects

1 of 26

Disable effects

View similar

Embed code

In contact with

Classmates

Telegram

Reviews

Add your review


Slide 1

Slide 2

A string is an array of characters, i.e. elements of type char. In Pascal, a string has a String data type. varName: string [Length]; If the length is not specified, memory is allocated for a string of up to 255 characters. Theory 1 var s1:string; string 255 characters var s2:string; string 20 characters

Slide 3

Rules: strings are equal only if they have the same set of characters and the same length; For example: "ABC"="ABC" and "ABC"≠"abc" otherwise an element-by-element comparison of characters by their codes occurs: "0"

Slide 4

Theory 3 Connecting two strings. s1:="2011" + "year"; Writeln(s1); s1:="10"; s2:="class"; s3:=s1+" "+s2; Writeln(s3);

Slide 5

Function Length The Length(S) function determines the current length of the string S. The result is an integer value. Theory 4 PROGRAM Length_1; VAR S: STRING; n:INTEGER; BEGIN Writeln('enterword "); Readln(S); n:= Length (S) ; Writeln('enterword ", n:5, " letters.."); END. We write the length of the string into a variable of integer type PROGRAM Dlina_2; VAR S: STRING; BEGIN Writeln('enter a word'); Readln(S); Writeln(’word consists of ’, Length (S),’letters. ’); END. We display the length of the string as a result of executing the function. Task 1. Create a program that counts the number of letters in a word.

Slide 6

Usescrt; var a,b:string; m,n:Integer; Begin Clrscr; Writeln("Enter the first line");Readln(a); Writeln("Enter the second line "); Readln(b); m:=Length(a); n:=Length(b); if (m=n) then writeln("Strings are equal"); if (m>n) then writeln("First is greater"); if (m

Slide 7

Soru function The Soru function (S, P, N) extracts from the string S a substring of N characters long, starting from position P. Here N and P are integer expressions. Theory 6 In the phrase MOTHER SOAPED RAMU, the word RAMU begins with the 11th letter and consists of 4 letters COPY(s1,11,4)

Slide 8

Task 3. Program n3_1; Usescrt; var a,b,c,d:string; Begin Clrscr; a:="computer science"; b:=""; c:=copy(a,8,1); d:=copy(a,4,2); b:=c+d+c; writeln(b); End. Task for composing strings 7 We use the function of working with symbolic variables COPY Second option: Program n3_2; Usescrt; var a,b:string; Begin Clrscr; a:=‘computer science"; b:=a+a+a+a; writeln(b); End. We use the definition of a string as an array of characters

Slide 9

Type Conversion 8 The STR Str(x, S) function converts the number x to string format. Where x is any numeric expression, S is a string variable. Task 4. Write a program that prints the first and last digit of a natural number entered from the keyboard. PROGRAM Primer; uses Crt; VAR S: STRING; n:INTEGER; BEGIN Write("Enter a number ");readln(n); Str(n,S); Writeln("First digit -",S); Writeln("The last digit is ",S); END. Convert a natural number to a string

Slide 10

The val procedure allows you to convert digital characters (the image of a number) into a number. In general, a call to a procedure looks like this: VAL (String, Number, Code) ; where String is a string constant or variable containing an image of a number; Number – a variable of integer or fractional type that must be assigned a value Code – error code returned by the procedure (integer) Type conversion 9 VAL procedure

Slide 11

Type conversion 10 BEGIN s1:="123456789"; val(s1,n,code); Writeln(n); END. BEGIN s1:="123456789ABCDE"; val(s1,n,code); Writeln(n); END.

Slide 12

s1:="ABCDE123456789"; val(s1,n,code); Writeln(n); PascalABC has a more advanced arsenal of transformations, like DELPHI: S:=IntToStr(N) - converts an integer to a string; n:=StrToInt(S) - converts a string to an integer S:=FloatToStr(r) - converts a real number to a string R:=StrToFloat(s) converts a string to a real number. If conversion is not possible, a run-time error occurs Type Conversion 11

Slide 13

Type conversion 12 Problem 5.

Slide 14

Symbols and their codes 13 Functions ORD and CHR Function Ord(S) – determines the serial number of the symbol. Function Chr(i) – determines the character with serial number i Using number 255, we derive the character from the code table I

Slide 15

Symbols and their codes 14 Task 6. Create a program that, based on a symbol entered from the keyboard, displays its number in the code table. PROGRAM Primer; uses Crt; VAR S: char; n,code:INTEGER; BEGIN Write("Enter character ");readln(s); Writeln("The symbol ", s," has the code -",ord(s)); END. ORD function works only with character data type

Slide 16

Problem 7. Program n5; UsesCrt; Var s:string; i,k:integer; beginClrScr; Write("IntoLine"); readln(s); k:=0; for i:=1 to length(s) do if (s[i]="A") then k:=k+1; writeln("The letter A occurs = ", k:8," times"); end. Iterate from 1 to the last letter If A is in the i-th place, increase K by 1 Counting characters in line 15

Slide 17

Note 16 for i:=1 to length(s) dobegin if (s[i]=…… if (s[i]=…… end; Note: If you need to search for several characters in a line, then we enclose the conditions in program brackets BEGIN ...END

Slide 18

Counting characters in line 17 Beginning Input S i,1,length(s) Si ="A" K=K+1 Output K End Yes No Flow diagram for the task of counting letters A in line K=0

Slide 19

Replacing characters in line 18 Task 8.Program n6; UsesCrt; Var s:string; i:integer; beginClrScr; Write("IntoLine"); readln(s); for i:=1 to length(s)do if (s[i]="A") then s[i]:="O"; writeln(s); end. We go through from 1 to the last letter. If in the i-th place there is A, we put it on i-th place ABOUT.

Slide 20

Replacing characters in line 19 Beginning Input S i,1,length(s) Si ="A" Si ="O" Output S End Yes No Flow diagram for the problem of replacing the letter A with O

Slide 21

Task 9 Remove all letters R (Latin, capital) from the string entered from the keyboard. Program n7; UsesCrt; var a,b:String; i:integer; beginClrScr; Write("InLine = ");readln(a); b:=""; for i:=1 to length(a) doif (a[i]'R") then b:=b+a[i]; a:=b; writeln(a); end. Iterate from 1 to the last letter Prepare additional variable If there is no R in the i-th place, then we add this letter to what is in B. What happened in B is moved back to A. Removing characters from string 20

Slide 22

Removing characters from string 21 Beginning Input A i,1,length(A) Ai "R" B=B+ Ai Output A End Yes No Flowchart for the problem of removing letters R from string B="" A=B

Slide 23

Nested conditions. 22 begin clRscr; Readln(s); for i:=1 to length(s) do if (s[i]="a") or (s[i]="b") then if (s[i]="a") then s[i] :="b" else s[i]:="a"; writeln(s); end. Problem 10. From the entire string we need to select only A and B and only then make the replacement

Slide 24

Nested conditions 23 Beginning Input S i, 1, length(S) Si ="a" Si ="b" Si ="a" Output S End Si ="a" or Si ="b" Yes No Yes No Block diagram to the problem of replacing A with B and vice versa

Slide 25

Nested loops 24 begin ClrScr; Write("IntoLine"); readln(s); s1:=",.;:"!?-"; for i:=1 to length(s1) do begin k:=0; for j:=1 to length(s) do if (s[j]=s1 [i]) then k:=k+1; if (k0) then writeln(s1[i]," - ",k:5," times"); end; end. Problem 11. Outer loop enumerating signs written in S1 The body of the outer loop contains a loop responsible for enumerating the characters of the entered string

Slide 26

Nested loops 25 Start Input S i, 1, length(S1) s1=",.;:"!?-" K=0 j, 1, length(S) Sj = S1i Yes No K=K+1 K0 Output S1i ,K No Yes End Flowchart for the problem of counting punctuation marks

View all slides

Abstract

�PAGE � �PAGE �1�

Block 1.

Theory.

Slide 1

Slide 2

Slide 3.

Slide 4

Slide 5. The solution to the problem has been analyzed:

Slide 6.

Slide 7.

Create a program that cuts out letters from the word COMPUTER SCIENCE so that they make the word CAKE.

Block 2.

Given a string of characters a1+a2=. Where 1<=а1 и а2<=9. Составить программу, вычисляющую это выражение. Измените программу так, что бы вместо знака «+» можно было писать « - », « * », « / ».

Problems for additional solution:

<=аi<=9). Составить программу, вычисляющую эту сумму.

<=аi<=9). Составить программу, вычисляющую эту алгебраическую сумму.

Block 3.

Block 4.

Given a string of characters. Determine how many times the letter A (Russian) appears in it.

Problems for additional solution:

Given a string of characters. Determine how many times the letter A (Russian) appears in it.

Block 5.

Given a string of characters. Replace all the letters A in it with the letter O.

Problems for additional solution:

Block 6.

Problems for additional solution:

Block 7.

In the given text, replace the letter “a” with the letter “b” everywhere, and the letter “b” with the letter “a”.

Given a character string containing punctuation marks. Count what and how many punctuation marks a line contains.

Problems for additional solution:

�PAGE � �PAGE �1�

Description for the presentation “Strings in Pascal”

This presentation can be used when studying the topic “Strings in the Pascal programming language” when teaching programming in the Pascal language. It discusses the definition and description of a string variable, functions for working with strings, and also examines several typical tasks when working with strings. The presentation contains 24 slides, combined into 7 blocks.

Brief navigation through the slides and additional tasks for students to solve independently:

Block 1.

Theory.

Slide 1 . The definition of a string variable is given, as well as the description method in the variable description section.

Slide 2 . The rules for comparing strings in the Pascal language are shown, and two examples illustrating these rules.

Slide 3. Examples of combining multiple lines into one are shown.

Slide 4 . The definition of the Length() function is given. And two solutions to the problem are analyzed:

Write a program to count the number of letters in a word.

The first method, using the assignment operation, assigns the number of characters in a string to a variable of integer type and displays it on the screen. In the second method, the Length() function is inserted directly into the Writeln statement.

Slide 5. The solution to the problem has been analyzed:

Compare the length of two strings entered from the keyboard.

In it, you need to determine the length of two strings and use the branch operator to compare the lengths of these strings.

Slide 6. The definition of the Copy() function is given. The screenshot shows an example of how this function works with the necessary comments.

Slide 7. Two ways to solve the problem are shown:

Create a program that cuts out letters from the word COMPUTER SCIENCE so that they make the word CAKE.

In the first case, cutting is done using the Copy function, and in the second, using the representation of the string as an array of characters.

Problems for additional solution:

Write a program that asks for a person's name and displays it on the screen with a greeting.

Write a program that asks for a person's first and last name and displays them on one line with the greeting.

Two lines are entered from the keyboard, determine which one is longer and by how much, and display the corresponding message on the screen.

A string of characters is entered from the keyboard, display the character in the kth place. k – entered from the keyboard.

A word is entered from the keyboard, is it correct that it begins and ends with the same letter?

Two words are given. Is it true that the first word ends with the same letter as the second one?

Get from the word "INTEGRAL" the words "DUMBELLS", "X-RAY", "TIGER", "AGENT".

Create a program that checks the spelling of the prefixes raz- and ras- in a word entered from the keyboard and, if necessary, corrects the error.

Block 2.

Type conversion functions.

Slide 8. A description of the Srt() procedure is given. The solution to the problem is analyzed:

Write a program that prints the first and last digit of a natural number entered from the keyboard.

In it, a natural number is converted into a string using the Str() procedure, and then the first and last character are displayed on the screen. Here it is imperative to tell students that this method is not suitable for every case, for example, in Olympiad problems, the condition is often set that a character data type cannot be used and/or a natural number contains more than 255 digits, etc.

Slide 9. The definition of the Val() procedure is given.

Slide 10. Shows examples of converting various strings containing numbers to a natural number.

Slide 11. Shows functions for converting numeric and character types in the PascalABC programming system. This can be very useful if you plan to switch to learning Lazarus or Delphi in the future.

Slide 12. The solution to the problem is shown:

Given a string of characters a1+a2=. Where 1<=а1 и а2<=9. Составить программу, вычисляющую это выражение. Измените программу так, что бы вместо знака «+» можно было писать « - », « * », « / ».

It considers the simplest case, when actions are performed with two natural single-digit numbers. In the future, depending on the level of the students, this task can be made more and more difficult. Adding multi-digit numbers, negative numbers, exponentiation, parentheses and ending with, for example, a calculation or conversion of Polish notation.

Problems for additional solution:

Given a string representing the decimal notation of a positive integer. Print a string representing the binary representation of the same number.

Given a string representing a positive integer. Find the sum of the digits of this number.

Given a text of the form a1+a2 +a3+…+ai. Where аi are integers (0<=аi<=9). Составить программу, вычисляющую эту сумму.

Given a text that has the form a1 - a2 + a3 - ... + ai. Where аi are integers (0<=аi<=9). Составить программу, вычисляющую эту алгебраическую сумму.

The number is given in hexadecimal number system. Print a line representing the decimal notation of the same number.

Given a string representing the binary representation of a positive integer. Print a line representing the decimal notation of the same number.

Block 3.

Functions for working with character codes.

Slide 13. The ORD and CHR functions are defined. A screenshot of the program illustrating the operation of the Chr function is shown.

Slide 14. The solution to the problem is shown:

Write a program that, based on a character entered from the keyboard, displays its number in the code table.

It is necessary to make a note here that the Ord function only works with the character data type (char). Because very often students do not understand the difference between character and string types.

Block 4.

Problems involving counting something in a line.

Slide 15. The solution to the problem is shown:

Given a string of characters. Determine how many times the letter A (Russian) appears in it.

This problem illustrates a way to search for characters in a string. It should be noted that to search for a string within a string, it is more appropriate to use the Copy function.

Slide 16. Contains a reminder that if you need to search for several characters in a line, then we enclose the conditions in program brackets BEGIN ... END

Slide 17. Shown is the flowchart for the task from slide 15.

Problems for additional solution:

Given a string of characters. Determine how many times the letter A (Russian) appears in it.

Given a string representing an algebraic expression, check the pairing of parentheses in it. Display the result on the screen.

The text is given. Determine how many identical neighboring letters it contains.

The text is given. Determine the number of words in a sentence.

The text is given. Determine which letters M or N there are more in it.

The text is given. Determine how many words in this text begin with the letter A.

Given a character string containing a colon (:). Determine how many characters precede it.

Given a string of characters. Determine how many times the group of letters ABC is included in it.

The text is given. It contains at least two letters K. Determine the serial numbers of the first and last letter K.

The text is given. Determine which of the letters C or T appears first.

The text is given. Determine the number of letters N preceding the first comma in the sentence.

Given a text containing two commas. Count the number of characters located between these commas.

Block 5.

Tasks for changing characters in a string without changing its length.

Slide 18. The solution to the problem is shown:

Given a string of characters. Replace all the letters A in it with the letter O.

This problem shows how you can change a string without changing its length.

Slide 19. Shown is the flowchart for the task from slide 18.

Problems for additional solution:

Given a string of characters. Replace all letters in even-numbered places with the letter O.

Given a string of characters. Replace the letter combination "min" with "max" in the given text.

Given a string of characters. Replace all spaces in it with the “_” sign.

Find the first and last letters K in the text entered from the keyboard. And replace them with "*".

Create a program that corrects the spelling of “ZHI” and “SHI” in a word entered from the keyboard.

Block 6.

Tasks related to changing the length of a line.

Slide 20. The solution to the problem is shown:

Remove all letters R (Latin, capital) from the string entered from the keyboard.

This task shows how a string changes as its length changes.

Slide 21. Shown is the flowchart for the task from slide 20.

Problems for additional solution:

Remove all letters A (Latin, capital) from the string entered from the keyboard.

The word has been given. Check if it is a shifter. That is, it reads the same from the end and the beginning. (For example: ANNA, SHALASH.)

In a given word, replace each letter “b” with the letter combination “ku”.

Write a program that removes any letters from a given text. (Enter text and letter from the keyboard).

To design headings and logically important words, text characters are alternated with spaces. Write a program that performs such an operation.

From this text, remove all the letters “C” that precede the letter “T”.

After each letter P, insert three exclamation marks “!!!”.

Block 7.

Problems with nested branches and loops.

Slide 22. The solution to the problem is shown:

In the given text, replace the letter “a” with the letter “b” everywhere, and the letter “b” with the letter “a”.

A direct solution to this problem “head-on” does not produce results. Here you need to select only the necessary letters so that the rest do not change. And then, depending on which letter is found, make a replacement using the full condition.

Slide 23. The flowchart for the task from slide 22 is shown.

Slide 24. The solution to the problem is shown:

Given a character string containing punctuation marks. Count what and how many punctuation marks a line contains.

This problem shows an example of using nested loops to compare two strings. It is possible to solve this problem using a large number of conditions, but such a solution, in my opinion, will not be rational.

Problems for additional solution:

Given a string of characters (Russian). Determine the number of vowels in this line.

Given a string of characters (English). Determine which and how many vowels the line contains.

Given a string of characters. Determine which characters have more vowels or consonants.

Given two strings A and B. Determine whether it is possible to form string B from the letters of string A. Moreover, each letter can be used once. Letters can be rearranged. (For example: you can get an AGENT from INTEGRAL, but not a GRAPH).

The text is given. Determine which has more numbers or letters.

Two lines are given. Count and display the number of common letters. (Letters appearing in the first and second lines).

From the given text, select and display those characters that appear in it exactly once.

To warn about the failure of the operation, Eustace came up with the following method of notification. He sends a telegram, the text of which contains the characters “b”, “e”, “d”, “a”, necessarily in that order, but not necessarily in a row. Create a program that allows you to determine whether the text sent is a failure notification or not.

To consolidate the topic, you need to solve as many problems as possible. Their number directly depends on the number of hours allocated to study a given topic and the programming course as a whole.

In my opinion, the most successful programming problem books are:

Collection of programming problems. 2nd edition. Author D.M. Zlatopolsky. Published in St. Petersburg "BHV-Petersburg" in 2007.

Electronic version of the Programming Taskbook Version 4.6. Author M.E.Abramyan. Distributed with the Pascal ABC programming system.

They present problems on all topics of the programming course.

Based on these problem books and some other sources, I made my own mini problem book dividing problems into three difficulty groups A, B and C.

http://privples.iv-edu.ru/informatica/1/Mater/SBORNIK_ZADAS.doc

Download abstract

To use presentation previews, create a Google account and log in to it: https://accounts.google.com


Slide captions:

Text and character data types Pascal The presentation was prepared by Computer Science Teacher Tatyana Petrovna Kareva MBOU Gymnasium No. 6 of Mezhdurechensk, Kemerovo Region

Computing machines deal with more than just numbers. They spend almost more time busy processing text. Pascal has a special data type for this, called CHAR (from the word character). Type CHAR (character or string or literal). Its meanings are individual characters: letters, numbers, signs. Character constants are enclosed in quotation marks, for example, 'A', 'B', 'C', '4','7', ' '(space). Character variables are described by the clause Var variable name: char ;

Character values ​​can be entered and output, assigned, and compared. Below is an example where all these steps are performed. Var x, y: char; Begin Write(‘ Enter character ‘); readln(x); Y:=’A’; If x

It is possible to compare symbols due to the fact that they are stored in computer memory as integers (character codes). Of two symbols, the one whose code is larger is considered larger. The characters are ordered as follows: ‘ A ’=,.

Standard symbolic functions. Pascal has standard symbolic functions: CHR (N) – returns the symbol with code N to the program, ORD (S) – returns the code of the symbol S, PRED (S) – returns the previous symbol SUCC (S) – returns the next symbol EXAMPLES: CHR ( 128) = B ORD (':') = 58 PRED(' B ') = A SUCC(' G ') = D

Each character has its own unique binary code. The codes of all symbols are summarized in a table. The first half of the table has become an international standard called ASCII - American Standard Code Information Interchange (read as “ask code”), among other things it contains the Latin alphabet, the second has different options for different languages. The Cyrillic alphabet (Russian alphabet) has several standards. Pascal uses the KOI-8 standard.

EXAMPLE of using a variable of character type. Create a program in which the computer repeatedly calculates the sum A + B for different values ​​of A and B. At the end of each stage, a request appears to continue or stop the calculations: “End the program? (Y/N).” Var A, B: real; C: char; Begin repeat Write(‘Enter two numbers’); Readln(a,b); Writeln(a + b:0:2); Writeln(‘End program?(Y/N)’); Readln(c); Until c='D'; (the program will exit if D is entered) Readln End.

Training tasks. 1 . What will the function CHR(ORD(X)) return? 2. Determine the values ​​of the following functions: CHR(68) ORD('d') PRED(1) SUCC(' I ') 3. Create a program in which the computer finds the product of odd numbers, starting from one, and until to the question asked after each calculation step: “Continue calculations? (Y/N)”, answer ‘Y’.

To handle larger text units - strings - a data type called STRING has been introduced. Values ​​of this type are strings of any characters up to 255 in length. String variables must be described by the clause: VAR name: STRING Strings can be assigned, compared, input, output, and concatenated. The connection is indicated by a "+" sign. Here are examples of some comparison operations on strings: "table"

Among all possible string values ​​there is an empty string. It is represented by two apostrophes (single quotation marks) with nothing between them. To enter this character into a string, you must repeat it twice. For example, the write operator ("about""phenomenon") will display: "declaration". The programmer has access to individual characters of a string variable; for this, in addition to the name of the variable, one must indicate the serial number of the character in the line. For example, if the variable X:STRING is described, then X is the first character of the string, X is the second, etc. X has a special role - to store the length of the string. The value of X is the character whose code is equal to the number of characters in the string. But to determine the length of a string variable, the LENGTH(string variable) function is usually used For example, if N:=LENGTH(x); - N will be assigned a value equal to the number of characters in the line.

When describing a string variable, we can limit the length of the string by specifying its maximum possible size, then only the specified number of characters will be stored in the string. Var a,b:string ; begin write("enter a word"); readln(a); write(a); readlnend. If you enter the word CORN while executing this program, the program will print COOK.

REMEMBER. If, during program execution, you need to enter a value for several string variables, each of them must have its own READLN input statement. For example, Var a,b,c:string ; begin readln(a); readln(b); readln(c); write(a+b+c); readlnend. Check what happens if you write READLN(a,b,c); or READ(a,b,c).

Example 1. Create a program that determines which of two surnames is longer. Last names have different lengths. Var a,b:string ; begin readln(a); readln(b); if length(a)>length(b) then write(a) else write(b); readlnend.

Example 2. Two words are given. Write a program to determine whether it is true that the first word begins with the same letter as the second word ends. Var x,y:byte ; a,b:string ; begin readln(a); readln(b); x:=length(b); (we determine the length of the word b to find out the number of the last character) if a=b[x] then write ("true") else write ("incorrect"); readln end .

Training tasks. 1. The name of the city is given. Determine whether the number of characters in it is even or odd. 2. The word is given. Display its third character and its last character twice. 3. The word is given. Is it true that it begins and ends with the same letter? 4. The word is given. Receive and display a letter combination consisting of its third and last character. 5. Create a program that requests the name of a football team and repeats it on the screen with the words: “This is the champion!”

To work with string variables in Pascal, there is a set of standard procedures and functions. Their use simplifies problem solving. I would like to remind you that the result of executing a function must be stored in a variable of the appropriate type, unless of course it, the function, is an element of the expression. Function for copying a string or part of it. S:=COPY(string, position, N); The copy function is also called "cutting". The result of executing the function will be a part of the string starting from the specified position of length N.

Example: A proposal is given. Determine the serial number of the first letter "k" encountered. If there is no such letter, please report it. Var x: integer; a:string; begin write("Enter a sentence"); readln(a); x:=pos(" to ",a); if x=0 then writeln ("There is no such letter") else writeln (x); readlnend.

Procedure for deleting part of a string DELETE(line, starting number, number of characters) Removes the specified number of characters from the source string. Example: Given a word consisting of an even number of letters. Display its first half. Var i,x:byte ; a,p:string ; begin repeat write ("Enter a word with an even number of letters"); readln(a); x:=length(a); (we determine the length of the word) until (x mod 2 = 0); x:= x div 2; (use integer division) delete (a,x+1,x); write(a); readln end .

Procedure for inserting a substring into a string INSERT(line1, line2, position); Line1 is inserted into line2 starting at the specified position. Training tasks. 1. A proposal is given. Determine the number of occurrences of a certain symbol. 2. A proposal is given. Replace all occurrences of the letter combination “ah” with “uh”. 3. The word is given. Check whether it is a “reversal”, i.e. reads the same way from the beginning to the end. 4. The word is given: a. remove the first letter “o” from it, if there is such a letter; b. remove the last letter "t" from it, if there is such a letter. 5. A proposal is given. Remove all the letters "s" from it.


Slide 1

Slide 2

A string is an array of characters, i.e. elements of type char. In Pascal, a string has a String data type. var Name: string [Length]; If the length is not specified, memory is allocated for a string of up to 255 characters. Theory 1 var s1:string; string 255 characters var s2:string; string 20 characters

Slide 3

Rules: strings are equal only if they have the same set of characters and the same length; For example: "ABC"="ABC" and "ABC"≠"abc" otherwise there is an element-by-element comparison of characters by their codes: "0"

Slide 4

Theory 3 Connecting two strings. s1:="2011" + "year"; Writeln(s1); s1:="10"; s2:="class"; s3:=s1+" "+s2; Writeln(s3);

Slide 5

Length Function The Length(S) function determines the current length of the string S. The result is an integer value. Theory 4 PROGRAM Length_1; VAR S: STRING; n:INTEGER; BEGIN Writeln('enter the word "); Readln(S); n:= Length (S) ; Writeln('enter the word ", n:5, " lit.."); END. We write the length of the string into a variable of integer type PROGRAM Dlina_2; VAR S: STRING; BEGIN Writeln('enter a word') ; Readln(S); Writeln('word consists of ', Length (S) ,'letters.'); END. We display the length of the string as a result of executing the function. Task 1. Create a program that counts the number of letters in a word.

Slide 6

Usescrt; var a,b:string; m,n:Integer; Begin Clrscr; Writeln("Enter the first line");Readln(a); Writeln("Enter the second line "); Readln(b); m:=Length(a); n:=Length(b); if (m=n) then writeln("The lines are equal"); if (m>n) then writeln("The first is greater"); if (m

Slide 7

Soru function The Soru function (S, P, N) extracts from the string S a substring of N characters long, starting from position P. Here N and P are integer expressions. Theory 6 In the phrase MOTHER SOAPED RAMU, the word RAMU begins with the 11th letter and consists of 4 letters COPY(s1,11,4)

Slide 8

Task 3. Create a program that cuts letters from the word COMPUTER SCIENCE so that they make the word CAKE. Program n3_1; Usescrt; var a,b,c,d:string; Begin Clrscr; a:="computer science"; b:=""; c:=copy(a,8,1); d:=copy(a,4,2); b:=c+d+c; writeln(b); End. Task for composing strings 7 We use the function of working with symbolic variables COPY Second option: Program n3_2; Usescrt; var a,b:string; Begin Clrscr; a:=‘computer science"; b:=a+a+a+a; writeln(b); End. We use the definition of a string as an array of characters

Slide 9

Type Conversion 8 The STR Str(x, S) function converts the number x to string format. Where x is any numeric expression, S is a string variable. Task 4. Write a program that prints the first and last digit of a natural number entered from the keyboard. PROGRAM Primer; uses Crt; VAR S: STRING; n:INTEGER; BEGIN Write("Enter a number ");readln(n); Str(n,S); Writeln("First digit -",S); Writeln("The last digit is ",S); END. Convert a natural number to a string

Slide 10

The val procedure allows you to convert digital characters (the image of a number) into a number. In general, a call to a procedure looks like this: VAL (String, Number, Code) ; where String is a string constant or variable containing an image of a number; Number – a variable of integer or fractional type that must be assigned a value Code – error code returned by the procedure (integer) Type conversion 9 VAL procedure

Slide 11

Type conversion 10 BEGIN s1:="123456789"; val(s1,n,code); Writeln(n); END. BEGIN s1:="123456789ABCDE"; val(s1,n,code); Writeln(n); END.

Slide 12

s1:="ABCDE123456789"; val(s1,n,code); Writeln(n); PascalABC has a more developed arsenal of transformations like DELPHI: S:=IntToStr(N) - converts an integer to a string; n:=StrToInt(S) - converts a string to an integer S:=FloatToStr(r) - converts a real number to a string R:=StrToFloat(s) converts a string to a real number. If conversion is not possible, a run-time error occurs Type Conversion 11

Slide 13

Slide 14

Symbols and their codes 13 Functions ORD and CHR Function Ord(S) – determines the serial number of the symbol. Function Chr(i) – determines the character with serial number i Using number 255, we derive the character from the code table I

Slide 15

Symbols and their codes 14 Task 6. Create a program that, based on a symbol entered from the keyboard, displays its number in the code table. PROGRAM Primer; uses Crt; VAR S: char; n,code:INTEGER; BEGIN Write("Enter character ");readln(s); Writeln("The symbol ", s," has the code -",ord(s)); END. ORD function works only with character data type

Slide 16

Problem 7. Given a string of characters. Determine how many times the letter A (Russian) appears in it. Program n5; UsesCrt; Var s:string; i,k:integer; beginClrScr; Write("Enter a string"); readln(s); k:=0; for i:=1 to length(s) do if (s[i]="A") then k:=k+1; writeln("The letter A occurs = ", k:8," times"); end. Iterate from 1 to the last letter If A is in the i-th place, increase K by 1 Counting characters in line 15

Slide 17

Note 16 for i:=1 to length(s) do begin if (s[i]=…… if (s[i]=…… end; Note: If you need to search for several characters in a line, then we enclose the conditions in program brackets BEGIN...END

Slide 18

Counting characters in line 17 Beginning Input S i,1,length(s) Si ="A" K=K+1 Output K End Yes No Flow diagram for the task of counting letters A in line K=0

Slide 19

Replacing characters in line 18 Problem 8. Given a string of characters. Replace all the letters A in it with the letter O. Program n6; UsesCrt; Var s:string; i:integer; beginClrScr; Write("Enter a string"); readln(s); for i:=1 to length(s) do if (s[i]="A") then s[i]:="O"; writeln(s); end. We go through from 1 to the last letter. If A is in the i-th place, put O in the i-th place.

Slide 20

Replacing characters in line 19 Beginning Input S i,1,length(s) Si ="A" Si ="O" Output S End Yes No Flow diagram for the problem of replacing the letter A with O

Slide 21

Task 9 Remove all letters R (Latin, capital) from the string entered from the keyboard. Program n7; UsesCrt; var a,b:String; i:integer; beginClrScr; Write("Enter string = "); readln(a); b:=""; for i:=1 to length(a) do if (a[i]'R") then b:=b+a[i]; a:=b; writeln(a); end. Iterate from 1 to the last letter We prepare an additional variable. If there is no R in the i-th place, then we add this letter to what is in B. We move what happened in B back to A. Removing characters from line 20

Publications on the topic