Data Structures and Algorithm
Name: Rana Muhammad Musab Akram Roll No : 23-CSE-24 Lab: 03
Lab Objective:
Implementation of the following string processing functions:
Length
Concatenation
Substring
Index
Abstract:
This lab focuses on implementing basic string processing functions, which are fundamental in
handling and manipulating text data in programming. The key operations performed include
calculating the length of a string, concatenating strings, extracting substrings, and finding patterns
(indexing). These operations are essential for efficient string manipulation and are widely used in data
processing, pattern recognition, and compiler construction. By translating algorithms into C++ code
and running various test cases, students gain hands-on experience in understanding internal string
operations.
Lab Tasks:
Run your coding of Algorithm A1 with the following test cases and record your observations below.
s = “A computer is an idiot machine”
s = “Data Structures is a prerequisite for compiler construction”
Run your coding of Algorithm A2 with the following test cases and record your observations.
s1 = “But” and s2 = “ter”
Data Structures and Algorithm
s1 = “Symmetric” and s2 = “Multiprocessing”
Run your coding of Algorithm A3 with the following test cases and record your observations.
s = “Cray is a supercomputer”, ip = 16 and len = 8
s = “Unix is a multi-user operating system”, ip = 24 and len = 6
Design an algorithm that takes a string S and a number n as input. It then concatenates S repeatedly to
itself until the length of the resultant string becomes greater than or equal to n.
Algorithm A5: repeatToLength(S, n)
1. START
2. Input string S and number n
Data Structures and Algorithm
3. Initialize an empty string RESULT ← ""
4. Repeat the following steps while length(RESULT) < n
a. RESULT ← RESULT + S
5. Output RESULT
6. END
f) Design an algorithm that takes a string S and a char a as input. It then displays the indices of all
occurrences of a in S.
Algorithm A6: findAllIndices(S, a)
1. START
2. Input string S and character a
3. Initialize index i ← 0
4. Repeat steps 5–6 while i < length(S)
a. If S[i] = a then
i. Display index i
b. i ← i + 1
5. END
Conclusion:
This lab helped in understanding and implementing fundamental string operations that are widely
applicable in software development and data processing. By converting algorithms into code and
testing with real inputs, we developed a clear understanding of how string handling functions like
length, concatenation, substring, and pattern matching work behind the scenes. These foundational
skills are critical for more advanced topics like text parsing, tokenization, and compiler design.