This document discusses strings and string operations in programming languages. It defines a string as a finite sequence of characters and notes that every programming language has a character set used for communication. It provides examples of strings with different lengths. The main string operations covered are concatenation, which joins two strings; substrings, which extract parts of strings; indexing/pattern matching to find the position of a substring; and calculating string length. A brute force algorithm for pattern matching is described that checks each character position for a match.
2. Basic Terminologies
Each programming language contains a character set that is used to
communicate with the computer, which usually includes:
▫ Alphabets: A –Z
▫ Digits: 0-9
▫ Special Characters: + - / * ( ) , . $ = ‘ whitespaces etc
▫ A finite sequence S of zero or more characters is called a String.
▫ The number of characters in a string is called its length.
▫ The string with zero characters is called the empty string or the null
string.
2
4. String Operations
Concatenation:
▫ Let S1 and S2 be two strings. The concatenation of S1 and S2, which
we denote by S1 || S2, is the string consisting of the characters of S1
followed by characters of S2.
▫ Eg: suppose S1= ‘MARK’ and S2= ‘TWAIN’ , Then
S1||S2= ‘MARKTWAIN’
And,
S1||’ ‘||S2= ‘MARK TWAIN’
4
5. String Operations
Substring:
▫ Given a string s of length N, a substring of s is a contiguous partition
of s that has length less than or equal to N.
Accessing a substring from a given string requires 3 pieces of information:
1. The name of the string or the string itself.
2. The position of the first character of the substring in the given string
3. The length of the substring or the position of the last character of the
substring.
5
7. String Operations
Indexing
▫ Also called pattern matching
▫ Finding the position where a string pattern P first appears in a given
string text.
▫ INDEX(string, pattern)
▫ Example:
▫ S= ‘HIS FATHER IS THE PROFESSOR’, then
INDEX (S,’THE’) have the value 7.
7
9. Pattern matching algorithm
▫ Pattern matching is the problem of deciding whether or not a given
string pattern P appears in a string text T.
▫ Assume that length of P doesn’t exceed length of T
▫ Brute Force algorithm/First Pattern matching algorithm.
9
11. ⬥ Let us take P= ‘GO’ and T= ‘ALGORITHM’
⬥ So, R will be 2 and S will be 9
⬥ Initially K=1, and MAX = 8
⬥ K<=MAX, true -> step 3.
⬥ L= 1 to 2 repeat,
⬥ P[1]!=T[1], { that is G != A}
⬥ then K=2,
⬥ Now K<=MAX, true -> step 3
⬥ L=1 to 2 repeat,
⬥ P[1]!=T[2], G!=L, then K=3
⬥ Now K<=MAX, true step 3,
⬥ L=1 to 2 repeat,
⬥ P[1]=T[3], G=G TRUE,
⬥ L=2
⬥ P[2]=T[3+2-1]= P[2]=T[4] ie. O=O
⬥ Index=3 and Exit.
⬥ Now we’ve Index of P in T at location 3
11
12. Exercises
You have the string text T=‘AROUND THE GLOBE’
1. Find index of pattern ‘THE’ in T
2. Find index of pattern ‘HER’ in T
12