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.
Group of consecutive elements in a string (such as word, phrases and sentences), called substring.
Part of string is called subtring.
‘ 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.
We call this operation SUBSTRING, specifically we write:
SUBSTRING(String, initial, length)
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:
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 :
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.
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
The function INDEX denoted in some programming languages as follows:
PL/1: INDEX(text, pattern)
PASCAL: POS(Pattern, text)
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’
S1//’ ‘//S2=‘DATA STRUCTURE’
Concatenation is denoted in some programming languages as follows:
The no of characters in a string is called its length.
We will write it as:
LENGTH(‘COMPUTER’) returns 8
Some of the programming languages denote Length function as follows:
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:
Inserting a string in the middle of the text.
INSERT(text, Position, String)
Deleting a substring from the text which begins in position K and has length L.
DELETE(text, position, length)
DELETE ( ‘ABCDEFG’,4,2) = ABCFG
DELETE(‘ABCDEFG’,0,4) = ABCDEFG
This function is used to replace the first occurrence of a Pattern P1, by the Pattern P2.
We will denote this operation by REPLACE.
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.