Created by Zaheer 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. Substring Indexing Concatenation 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. The name of string or string itself. The position of first character of substring  and 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 7 INDEX(T,’THEN’) returns 0 INDEX(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: S 1 //S 2  where S 1  is first string and S 2  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//S2 FORTRAN:  S1//S2 BASIC:   S1+S2 C:   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: Insertion Deletion: 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’ DELETION Deleting 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.

Lect 14 Zaheer Abbas

  • 1.
    Created by ZaheerAbbas Aghani
  • 2.
  • 3.
    Strings Operations Aswe 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. Substring Indexing Concatenation Length
  • 4.
    SUBSTRING Group ofconsecutive 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. The name of string or string itself. The position of first character of substring and The length of substring.
  • 5.
    SUBSTRING CONT… Wecall 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)
  • 6.
    INDEXING Indexing, alsocalled 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 7 INDEX(T,’THEN’) returns 0 INDEX(T,’ THE ’) returns 14
  • 7.
    INDEXING CONT… Thefunction INDEX denoted in some programming languages as follows: PL/1: INDEX(text, pattern) PASCAL: POS(Pattern, text)
  • 8.
    CONCATENATION Concatenation meansto 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: S 1 //S 2 where S 1 is first string and S 2 is second. Example: suppose S1 = ‘DATA’ and S2=‘STRUCTURE’ then S1//S2=‘DATASTRUCTURE’ but S1//’ ‘//S2=‘DATA STRUCTURE’
  • 9.
    CONCATENATION CONT… Concatenationis denoted in some programming languages as follows: PL/1: S1//S2 FORTRAN: S1//S2 BASIC: S1+S2 C: strcat(S1,S2)
  • 10.
    LENGTH Theno 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)
  • 11.
    WORD PROCESSING OPERATIONSIn 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: Insertion Deletion: Replacement:
  • 12.
    INSERTION Inserting astring in the middle of the text. EXAMPLE: INSERT(text, Position, String) INSERT(‘ABCDEFG’,3,’XYZ’)=‘ABXYZCDEFG’ INSERT(‘ABCDEFG’,6,’XYZ’)= ‘ABCDEXYZFG’ DELETION Deleting 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
  • 13.
    REPLACEMENT This functionis 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.