Strings
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
Examples
▫ String
‘DATA STRUCTURES’ //length - 15
‘HOW ARE YOU?’ //length – 12
‘ ‘ //length – 1
‘’ //length – 0
3
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
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
▫ SUBSTRING(string, initial, length)
▫ Example:
▫ SUBSTRING(‘MARK TWAIN’,7,4) = ‘WAIN’
6
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
String Operations
Length
▫ LENGTH(string)
▫ Example: LENGTH(‘COMPUTER’)=8
8
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
10
⬥ 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
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
THANKS!
ANY QUESTIONS?
Feel free to contact!!!
13

Strings

  • 1.
  • 2.
    Basic Terminologies Each programminglanguage 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
  • 3.
    Examples ▫ String ‘DATA STRUCTURES’//length - 15 ‘HOW ARE YOU?’ //length – 12 ‘ ‘ //length – 1 ‘’ //length – 0 3
  • 4.
    String Operations Concatenation: ▫ LetS1 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: ▫ Givena 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
  • 6.
    ▫ SUBSTRING(string, initial,length) ▫ Example: ▫ SUBSTRING(‘MARK TWAIN’,7,4) = ‘WAIN’ 6
  • 7.
    String Operations Indexing ▫ Alsocalled 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
  • 8.
    String Operations Length ▫ LENGTH(string) ▫Example: LENGTH(‘COMPUTER’)=8 8
  • 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
  • 10.
  • 11.
    ⬥ Let ustake 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 thestring text T=‘AROUND THE GLOBE’ 1. Find index of pattern ‘THE’ in T 2. Find index of pattern ‘HER’ in T 12
  • 13.