Lect 14 Zaheer Abbas

Post on 19-May-2015

325 views 0 download

Transcript of Lect 14 Zaheer Abbas

Created byZaheer Abbas Aghani

LECTURE 14

STRINGS OPERATIONS

Strings Operations As we know that a string may be viewed simply a sequence of characters or

linear array of characters.

For processing with such array of characters various string operations have been developed which are not normally used with other kinds of arrays.

These operations are called string oriented operations.

Following are four basic string oriented operations.1. Substring2. Indexing3. Concatenation4. Length

SUBSTRING

Group of consecutive elements in a string (such as word, phrases and sentences), called substring.

OR Part of string is called subtring. For example:

‘TO BE OR NOT TO BE’ is a string and ‘TO BE OR’ are substring

Accessing a substring from a given string requires three pieces of information.i. The name of string or string itself.ii. The position of first character of substring andiii. The length of substring.

SUBSTRING CONT… We call this operation SUBSTRING, specifically we write:

SUBSTRING(String, initial, length) Examples:

SUBSTRING(‘TO BE OR NOT TO BE’, 4,7) = ‘BE OR N’SUBSTRING(‘THE END’, 4,4) = ‘ END’

Substring function is denoted in some programming languages as follows:

PL/1: SUBSTR(S,4,7)FORTRAN: S(4:10)PASCAL: COPY(S,4,7)BASIC: MID$(S,4,7)

INDEXING Indexing, also called pattern matching refers to finding the position where a string

pattern first appears in a given string text. We call this function INDEX and we write as :

INDEX(text,pattern) This function returns an integer value. If the pattern appears, INDEX return its position else if pattern does not appear in

the string text, the INDEX is assigned the value 0. Example:

suppose T contain the text=‘HIS FATHER IS THE PROFESSOR’ then

INDEX(T,’THE’) returns 7INDEX(T,’THEN’) returns 0INDEX(T,’ THE ’) returns 14

INDEXING CONT…

The function INDEX denoted in some programming languages as follows:

PL/1: INDEX(text, pattern)PASCAL: POS(Pattern, text)

CONCATENATION

Concatenation means to add to the end, or to append. This function adds one string at the end of another string.

Concatenation function is denoted by // For example: S1//S2 where S1 is first string and S2 is second.

Example: suppose S1 = ‘DATA’ and S2=‘STRUCTURE’ then

S1//S2=‘DATASTRUCTURE’ but

S1//’ ‘//S2=‘DATA STRUCTURE’

CONCATENATION CONT…

Concatenation is denoted in some programming languages as follows:

PL/1: S1//S2FORTRAN: S1//S2BASIC: S1+S2C: strcat(S1,S2)

LENGTH The no of characters in a string is called its length. We will write it as:

LENGTH(String)

Example:LENGTH(‘COMPUTER’) returns 8

Some of the programming languages denote Length function as follows:

PL/1: LENGTH(String)BASIC: LEN(String)PASCAL: LENGTH(String)C: strlen(String)

WORD PROCESSING OPERATIONS

In earlier times, character data processed by the computer mainly of data items, such as names and address. today the computer also processes printed matter, such as letters, articles and reports. This is called word processing.

Word processing can also be define as:

“The creation, input, editing and formatting of documents and other text using software on a computer.”

The operations usually associated with the word processing are the following:

a) Insertionb) Deletion:c) Replacement:

INSERTION Inserting a string in the middle of the text. EXAMPLE:

INSERT(text, Position, String)INSERT(‘ABCDEFG’,3,’XYZ’)=‘ABXYZCDEFG’INSERT(‘ABCDEFG’,6,’XYZ’)= ‘ABCDEXYZFG’

DELETIONDeleting a substring from the text which begins in position K and has length L.EXAMPLE:

DELETE(text, position, length)

DELETE(‘ABCDEFG’,4,2) = ABCFG

DELETE(‘ABCDEFG’,0,4) = ABCDEFG

REPLACEMENT

This function is used to replace the first occurrence of a Pattern P1, by the Pattern P2.

We will denote this operation by REPLACE. Example:

REPLACE(text, P1,P2)REPLACE(‘ABCDEFG’,’AB’,’C’) = CCDEFG.REPLACE(‘XABYABZ’,’BA’,’C’) = XABYABZ.

In second example, the pattern BA does not occur and hence there is no change.