DSA Assignment 01
DSA Assignment 01
5: Language Syntax:
Strings are essential for defining the syntax and semantics of programming
languages.
Strings Applications:
1: Text Processing:
a: Word processors
b: Text editors
c: Search engines
d: Natural language processing
2: Web Development:
a: HTML, CSS, and JavaScript
b: Web frameworks
c: Web servers
3: Database Systems:
a: Storing and querying textual data
b: Data mining and analysis
4: Operating Systems:
a: Command-line interfaces
b: File systems
c: User interfaces
5: Game Development:
a: Dialogue and story elements
b: User input and output
2: String Operations:
Write code to perform the following operations on strings:
1: Concatenation
2: Length determination
3: Substring extraction
4: Searching for a substring
5: Reversing a string
6: Counting occurrences of a character in a string
Code:
#include <iostream>
#include <algorithm>
using namespace std;
void Concate();
void Length();
void substring();
void search_substring();
void reverse();
void count_char();
int main(){
int n;
cout << "Enter ur choice:\n1 for Concatenation\n2 for Length determination\n"
<< "3 for Substring extraction\n4 for Searching for a substring\n"
<< "5 for Reversing a string\n6 for Counting no. of a character in a string"
<< "\n7 for Exit\n";
cin >> n;
if (n == 1){
Concate();
} else if (n == 2){
Length();
} else if (n == 3){
substring();
} else if (n == 4){
search_substring();
} else if (n == 5){
reverse();
} else if (n == 6){
count_char();
} else if (n == 7){
cout << "Exit";
return 0;
}
cout << "\nDo U wanna try other choices!!";
return 0;
}
void Concate(){
string str1 = "Kamal";
string str2 = "Shah";
string str3 = str1 + str2;
cout << "The Concatenated String is:" << str3;
}
void Length(){
string str = "Kamal Shah";
int str_length = str.length();
cout << "The str string length is:" << str_length;
}
void substring(){
string original = "Kamal Shah";
string sub_str = original.substr(6,4);
cout << "The Substring is:" << sub_str;
}
void search_substring(){
string str = "Kamal Shah";
string substr = "Shah";
int found = str.find(substr);
if (found == -1){
cout << "The substring is not Present in a String";
} else {
cout << "The substring is present at indux:" << found;
}
}
void reverse(){
string str = "Bachah";
reverse(str.begin(), str.end());
cout << "Reversing the string:" << str;
}
void count_char(){
string str = "Kamal Shah";
char to_search = 'a';
int presence = 0;
for (int i = 0; i < str.length(); i++){
if (str[i] == to_search){
presence++;
}
}
cout << "The Character is present " << presence << " time in a String";
}
Output:
Explain the time complexity of each operation and analyze the
efficiency of the algorithms used?
1: Concate():
Efficiency: Efficient for small strings but the operation becomes costly for larger
strings due to the need to create a new string.
2: Length():
3: Substring():
Efficiency: Efficient for small substrings but may become slower for very large
substrings.
4: Searching substring():
5: Reverse():
Efficiency: Efficient for moderate sized strings but could be optimized for larger
datasets by using frequency tables.
Length-Prefixed Strings:
The first part of the memory stores the string's length followed by the characters.
String literals are fixed strings embedded directly in the program binary during
compilation. String literals are immutable in many programming languages to ensure
safety.
Example:
‘Hello’ is stored in read-only memory and its address is returned when used in code.
Strings can be allocated in the heap at runtime, allowing flexibility for variable-
length strings. Requires explicit allocation (new in C++ or malloc() in C) and
deallocation (delete).
Example:
char* my_string = (char*) malloc (20 * sizeof(char));
strcpy(my_string, "Dynamic String");
free(my_string);
Length-Prefixed Strings: The first part of memory contains the length of the
string, followed by the actual characters.
Advantages:
a: Efficiency: Length is known instantly (O(1)).
b: Safety: Easier to avoid buffer overflows since length is explicitly stored.
Disadvantages:
a: Compatibility: Cannot be used directly with C-style string functions.
b: Overhead: Additional space is required to store the length.
4: Practical Applications:
a: Choose a real-world application of strings in computer science (e.g.,
text processing, searching algorithms, or data compression) and explain
how strings are used in that context?
b. Discuss any data structure choices that can enhance the performance
of string related operations in the selected application?
The performance of string searching operations can be greatly improved using
specialized data structures. Some common choices are;
Advantages:
1: Efficient for pattern matching (O(m) where m is the length of the pattern).
2: Useful for multiple string operations like longest repeated substring & LCS etc.
Drawbacks:
1: High space complexity (O(n^2) in the worst case for naive implementations).
Advantages:
1: Space-efficient compared to suffix trees.
2: Supports pattern searching using binary search (O(m⋅logn)).
Drawbacks:
1: Slower than suffix trees for some operations.
c: Trie (Prefix Tree): A tree where each node represents a character of a string,
with paths forming words or prefixes.
Advantages:
1: Efficient for prefix searches.
2: Space-efficient for storing a dictionary of strings.
Drawbacks:
1: Slower for suffix operations compared to suffix trees.
Advantages:
1: Fast average-case lookup time (O(1)).
2: Useful for checking if a substring exists in a dataset.
Drawbacks:
1: Collisions can degrade performance.