0% found this document useful (0 votes)
13 views

Lab Report 03

Uploaded by

akammiea Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab Report 03

Uploaded by

akammiea Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ABSTRACT (LAB TASK 03)

String Operations

Md. Ahadur Rahman Munshi


EMCS-1108026

DATA STRUCTURE &


ALGORITHM LAB
EMCS-526
Problem Statement 01:
String Indexing (Pattern Matching)

Analysis:
Indexing also called pattern matching refers to finding the position where a string pattern P first
appears in a given string text T.

Algorithm:
FUNCTION find_pattern_index(string, pattern):
string_length = LENGTH(string)
pattern_length = LENGTH(pattern)

IF pattern_length > string_length:


RETURN -1 # Pattern is longer than the string, no match possible

# Iterate through the string to find the pattern


FOR i FROM 0 TO string_length - pattern_length:
# Check if the substring of the same length as pattern matches
substring = string[i : i + pattern_length]

IF substring == pattern:
RETURN i # Return the index where the pattern first matches

RETURN -1 # If no match is found, return -1

Source Code:
Input-Output

01

02
Problem Statement 02:
String INSERTION, DELETION & REPLACEMENT

Analysis:
String INSERTION refers to the process of inserting a substring or a character at a specific position
within an existing string. In programming, many languages offer built-in functions or methods to
facilitate string insertion, but the process generally involves splitting the string, inserting the new
substring, and then combining the parts back together.

String DELETION refers to the process of removing a specific portion (a substring or character)
from a given string. Similar to string insertion, deletion involves identifying a position (or range of
positions) in the string and modifying the string by removing the specified part.

String REPLACEMENT involves replacing part of a string (a substring or character) with another
string or character. String replacement can be used to modify a string by finding and replacing
substrings, either at specific positions or based on the occurrence of a particular pattern.

Algorithm:
INSERTION
Split the original string into two parts:
• The part before the insertion point.
• The part after the insertion point.
Insert the new substring at the desired position.
Concatenate the parts (including the new substring) to form the final string.

DELETION
Identify the portion of the string to be deleted (using position and length or a specific substring).
Remove the specified portion.
Concatenate the remaining parts of the string.

REPLACEMENT
Identify the portion of the string (a substring or character) to be replaced.
Replace the identified portion with the new substring.
Return the updated string.

Source Code:

Input-Output

01
Problem Statement 03:
String Pattern Matching

Analysis:
String Pattern Matching is a fundamental operation where a pattern (or substring) is searched within
a larger string (or text). The goal is to find the occurrences of the pattern within the string or determine
if the pattern exists in the string. The simplest way to perform pattern matching is the naive approach,
where you slide the pattern over the text one character at a time and check for matches.
Example:
Let’s say we want to find occurrences of the pattern P = "abc" in the string T = "ababcabc".
Naive Algorithm:
• Start at the beginning of the text.
• For each position, compare the pattern with the substring of the same length in the text.
• If they match, record the position.
• Slide the pattern by one character and repeat until the end of the text.

Algorithm:
function NaivePatternMatching(T, P)
n = length of text T
m = length of pattern P

for i = 0 to (n - m)
j=0
while j < m and T[i + j] == P[j]
j=j+1

if j == m
print "Pattern found at position " i

Source Code:

Input-Output

01

02
Problem Statement 04:
String Pattern Matching

Analysis:
This algorithm uses a finite automaton to match the pattern. The automaton processes one character
of the text at a time and updates its state accordingly. The states represent how much of the pattern
has been matched so far. If the automaton reaches the final state, it means the pattern has been fully
matched in the text.
Example:
Let’s say we want to search for the pattern P="abc" in the text T="xabcy".
1. The automaton starts at state Q0 (no characters matched yet).
2. The algorithm reads each character from the text and uses the transition function F to update
the state.
3. If the automaton reaches the final state after processing the pattern, the pattern has been found
in the text, and the starting index is computed.
4. If the automaton never reaches the final state, the pattern does not exist in the text.
This approach is efficient and avoids unnecessary re-comparisons that might occur in a naive pattern-
matching algorithm. It precomputes the state transitions in a way that allows for faster matching.

Algorithm:

Source Code:

Input-Output

01
Problem Statement 05:
In Programming problems assume the preface of this text is stored in a linear array LINE such that
LINE[K] is a static character variable storing 80 characters and represents a line of the preface.
Assume that each paragraph begins with 5 blank spaces and there is no other indention. Also, assume
there is a variable NUM which gives the number of lines in the preface. Now, write a program which
reads a given WORD and then counts the number of C of times WORD occurs in LINE. Test the
program using (a) WORD = “THE” and (b) WORD = “HENCE”.

Analysis:
• NUM gives the number of lines in the preface.
• Each line of text is stored in LINE (an array of strings).
• Each paragraph starts with 5 blank spaces.
• The goal is to count occurrences of the given WORD in the preface.

Algorithm:
function NaivePatternMatching(T, P)
n = length of text T
m = length of pattern P

for i = 0 to (n - m)
j=0
while j < m and T[i + j] == P[j]
j=j+1

if j == m
print "Pattern found at position " i

Source Code:

Input-Output

01

02

03
Problem Statement 06:
In Programming problems assume the preface of this text is stored in a linear array LINE such that
LINE[K] is a static character variable storing 80 characters and represents a line of the preface.
Assume that each paragraph begins with 5 blank spaces and there is no other indention. Also, assume
there is a variable NUM which gives the number of lines in the preface. Now, write a program which
interchanges the Jth and Kth paragraphs. Test the program using J=2 and K=4.

Analysis:
Identify Paragraphs: Since each paragraph begins with 5 spaces, we can determine where each
paragraph starts by checking for these spaces in the lines.
Store Paragraphs: Extract each paragraph into an array or list for easy manipulation.
Swap Paragraphs: Interchange the contents of the Jth and Kth paragraphs.
Reconstruct the Output: Finally, we reconstruct the LINE array with the new order of paragraphs.

Algorithm:
• Initialize a 2D array lines with the preface text.
• Call printPreface to display the original text.
• Call interchangeParagraphs to swap the specified paragraphs.
• Call printPreface again to display the modified text.

Source Code:

Input-Output

01
Problem Statement 07:
Assume the preface of this text is stored in a single character variable TEXT. Assume 5 blank spaces
indicates a new paragraph. Now, write a program which constructs a linear array PAR such that
PAR[K] contains the location of Kth paragraph in TEXT, and which finds the value of a variable
NPAR which contains the number of paragraphs.

Analysis:
The task involves parsing a text stored in a single character array to identify the starting indices of
paragraphs. A paragraph is defined as a segment of text that begins with five leading spaces. The goal
is to:
1. Construct an array (PAR) that stores the starting index of each paragraph.
2. Determine the total number of paragraphs (NPAR).

Algorithm:
Input Text Parsing:
• Read the input text stored in the TEXT variable.
Check for Paragraph Indicators:
• For each character in TEXT, check if the current character and the next four characters are
spaces (' ').
• If found, store the starting index in the PAR array.
Skipping to the Next Paragraph:
• After identifying a paragraph, move the index forward to skip the five spaces and then
continue until the end of the paragraph (until a newline character is found).
Count and Store:
• Maintain a count of how many paragraphs have been identified and store each paragraph's
starting index.

Source Code:

Input-Output

01

02

You might also like